## Set 2D array elements based on 2D index array

how to assign values to two dimensional array in java

two-dimensional array example

2d array java

sum of elements in 2d array in c

2d array python

how to take input in 2d array in c++

initialize a 2d array

In Python, say you have a 2D array of zeros of shape (N,4,4):

z = array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]])

and you have a 2D array of indices:

i = array([[1, 1, 1, 1], [1, 0, 0, 0], [0, 0, 1, 1], [1, 1, 1, 0]])

and some valued 2D array:

v = array([[ 2., 4., 10., 7.], [10., 9., 9., 2.], [ 3., 8., 8., 8.], [ 8., 6., 10., 1.]])

Is there a way to fill the elements of `z`

with the values of `v`

but in the slices denoted by `i`

, without using loops?

Note: Is there a way to do this in a scalable fashion such that if you had an N channel array `z`

, where N>>1, you would not need to directly index `z[i]`

when filling it with values from `v`

?

For clarity, the resulting `z`

array would look like the following:

z = array([[[0., 0., 0., 0.], [0., 9., 9., 2.], [3., 8., 0., 0.], [0., 0., 0., 1.]], [[2., 4., 10., 7.], [10., 0., 0., 0.], [0., 0., 8., 8.], [8, 6., 10., 0.]]])

Obviously, using something like:

z = v[i,:,:]

would never work but maybe there's a way to use `rows, cols`

to do this?

Many thanks in advance!

Edit:

For clarity here is a similar example but for a 3D `z`

as requested in the comments:

z = array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]]) i = array([[2, 1, 2, 1], [1, 1, 2, 1], [1, 1, 1, 1], [1, 0, 0, 1]]) v = array([[5., 5., 0., 4.], [4., 6., 8., 3.], [4., 0., 4., 8.], [7., 6., 5., 7.]])

`z`

would become:

z = array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 6., 5., 0.]], [[0., 5., 0., 4.], [4., 6., 0., 3.], [4., 0., 4., 8.], [7., 0., 0., 7.]], [[5., 0., 0., 0.], [0., 0., 8., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]])

You can always use masks and multiply:

from numpy import array z = array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]]) i = array([[1, 1, 1, 1], [1, 0, 0, 0], [0, 0, 1, 1], [1, 1, 1, 0]]) v = array([[ 2., 4., 10., 7.], [10., 9., 9., 2.], [ 3., 8., 8., 8.], [ 8., 6., 10., 1.]]) z[0] = ~i.astype(bool)*v z[1] = i.astype(bool)*v

Output:

z = array([[[ 0., 0., 0., 0.], [ 0., 9., 9., 2.], [ 3., 8., 0., 0.], [ 0., 0., 0., 1.]], [[ 2., 4., 10., 7.], [10., 0., 0., 0.], [ 0., 0., 8., 8.], [ 8., 6., 10., 0.]]])

For more general case:

for ax in np.unique(i): mask = i == ax z[ax] = mask*v print(z)

Output:

[[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 6. 5. 0.]] [[0. 5. 0. 4.] [4. 6. 0. 3.] [4. 0. 4. 8.] [7. 0. 0. 7.]] [[5. 0. 0. 0.] [0. 0. 8. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]]

Note: Here I assumed the values in `i`

are same as there are channels in `z`

. If different, instead of np.unique, you can try:

for i_val,ax in zip(np.unique(i),range(z.shape[0])):: mask = i == i_val z[ax] = mask*v

**EDIT**

A one-liner would be:

>>> z = np.array([*map(i.__eq__,np.unique(i))])*np.array([v]*z.shape[0]) >>> z array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 6., 5., 0.]], [[0., 5., 0., 4.], [4., 6., 0., 3.], [4., 0., 4., 8.], [7., 0., 0., 7.]], [[5., 0., 0., 0.], [0., 0., 8., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]])

**[PDF] Two-Dimensional Arrays,** dimensional arrays, a single line of elements. • Often data Two-dimensional ( 2D) arrays are indexed by two assign the reference to the new array to rating:. We can say that a 2d array is an array of array. A multidimensional array is mostly used to store a table-like structure. In today’s topic, we are going to see this 2-dimensional array. 2 dimensional Array has two pairs of square brackets. first one for row and the second one for the column.

Something more complicated without numpy:

r = list(zip(i,v)) z[0] = [[l2[i] if l1[i]==0 else 0 for i in range(4)] for l1, l2 in r ] z[1] = [[l2[i] if l1[i]==1 else 0 for i in range(4)] for l1, l2 in r ]

**Two dimensional (2D) arrays in C programming with example,** For now don't worry how to initialize a two dimensional array, we will discuss the elements entered by user in a 2d array and how to display the elements of a array name works as a pointer to the base element (first element) of the array. An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. Before we discuss more about two Dimensional array lets have a look at the following C program. Simple Two dimensional(2D) Array Example

Possible using `numpy.where`

:

z[0] = np.where(i, z[0], v) z[1] = np.where(1 - i, z[1], v)

Output:

array([[[ 0., 0., 0., 0.], [ 0., 9., 9., 2.], [ 3., 8., 0., 0.], [ 0., 0., 0., 1.]], [[ 2., 4., 10., 7.], [10., 0., 0., 0.], [ 0., 0., 8., 8.], [ 8., 6., 10., 0.]]])

And after your update, if `i`

hold the indices of `z`

to be changed (and opposite from the 0's and 1's in your example), it can be scaled easily to:

for N in range(z.shape[0]): z[N] = np.where(i == N, v, z[N])

Or if you really want to avoid loops alltogheter:

z = np.where(i == (np.where(np.ones(len(z.reshape(-1))) == 1)[0] // (z.shape[1] * z.shape[2])).reshape(z.shape), v, z)

Ouput:

array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 6., 5., 0.]], [[0., 5., 0., 4.], [4., 6., 0., 3.], [4., 0., 4., 8.], [7., 0., 0., 7.]], [[5., 0., 0., 0.], [0., 0., 8., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]])

**Accessing Data Along Multiple Dimensions in an Array — Python ,** Topic: Indexing into multi-dimensional numpy arrays, Difficulty: Easy, Category: Section. Define the “dimensionality” of an array. Similar to Python's sequences, we use 0-based indices and slicing to access the content of an array. providing two numbers to access an element # in a 2D-array >>> grades[1, 0] # Brad's� If you want to index that same 2D array also by column, you can resize the function to show another set of input terminals. This next set of inputs has its own corresponding subarray output. By default, if you do not wire any index input terminals, the first subarray indexes row 0, the second subarray indexes row 1, and so on.

**Two-Dimensional Arrays \ Processing.org,** A two-dimensional array is really nothing more than an array of arrays (a 2D array int[][] myArray = new int[cols][rows]; // Initialize 2D array values for (int i = 0;� The variations Array.iteri and Array.iteri2 allow the index of the element to be involved in the computation; the same is true for Array.mapi and Array.mapi2. The functions Array.fold , Array.foldBack , Array.reduce , Array.reduceBack , Array.scan , and Array.scanBack execute algorithms that involve all the elements of an array.

**How to declare and Initialize two dimensional Array in Java with ,** Just like a normal array, the index starts at 0 and finishes at length -1. You can verify this by accessing the first element of a two-dimensional array as multiples[0 ][0], You can use it on tile-based game like Super Mario Bros to draw terrain,� Introduction to 2D Arrays In Python. Arrangement of elements that consists of making an array i.e. an array of arrays within an array. A type of array in which the position of a data element is referred by two indices as against just one, and the entire representation of the elements looks like a table with data being arranged as rows and columns, and it can be effectively used for performing

**Javanotes 8.1, Section 7.5 -- Two-dimensional Arrays,** The elements of a 2D array are arranged in rows and columns, and the new If we use int[] as the base type, the type that we get is "int[][] meaning "array of int[]" with no number in the second set of brackets, creates an array of 3 elements (You should make sure that you understand how the array indexing works here.). Select a copy of row at index 1 from 2D array and set all the elements in selected sub array to 100 # Select a copy of row at index 1 from 2D array row = nArr2D[1].copy() # Set all the elements in selected sub array to 100 row[:] = 100 Here, sub array is a copy of original array so, modifying it will not affect the original Numpy Array

##### Comments

- Thanks for the reply, unfortunately this wouldn't be scalable for high dimensional versions of
`z`

with many many channels. That's my fault for not being clear enough in he question though so I've updated the text accordingly. - Could you provide an example for what you want for
`z`

with 3 channels? - Thanks! I tried working that through and it all looks good :D
- Glad to help :)
- If the 4D array is
`X`

then,`for k in range(X.shape[0]):X[k] = np.array([*map(i.__eq__,np.unique(i))])*np.array([v]*X[k].shape[0])`

- Thanks, this works but with loops unfortunately.. so not the ideal case but if it can't be done without loops then it's currently the best option.
- @user8188120 it has a nested-loop for
**each**of the z[0], z[1]... etc. It is defenitally**not**better than`numpy`

solutions who don't have a single loop for each of the z[0], z[1]... etc. - Thanks for the reply, unfortunately this wouldn't be scalable for high dimensional versions of
`z`

with many many channels as in the true problem I won't be able to index`z[i]`

if there were say hundreds of channels. That's my fault for not being clear enough in he question though so I've updated the text accordingly. - @user8188120 Edited