Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two arrays defining 2D coordinates, as array indices

I have a 2D array, call it A. I have two other 2D arrays, call them ix and iy. I would like to create an output array whose elements are the elements of A at the index pairs provided by ix and iy. I can do this with a loop as follows:

for i=1:nx
    for j=1:ny
        output(i,j) = A(ix(i,j),iy(i,j));
    end
end

How can I do this without the loop? If I do output = A(ix,iy), I get the value of A over the whole range of (ix)X(iy).

like image 390
Jason Avatar asked Mar 12 '10 18:03

Jason


People also ask

How are 2D arrays indexed?

Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Each element in the 2D array must by the same type, either a primitive type or object type.

What does the two index values of a 2 dimensional array represent?

Two dimensional array is an array within an array. It is an array of arrays. In this type of array the position of an data element is referred by two indices instead of one. So it represents a table with rows an dcolumns of data.

Can multi dimensional arrays be indexed?

Indexing multi-dimensional arraysMulti-dimensional arrays are indexed in GAUSS the same way that matrices are indexed, using square brackets [] . Scanning above, you can see that the value of the element at the intersection of the third row and second column of x1 is 8.

How are 2D Numpy arrays indexed?

Indexing a Two-dimensional Array To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value '10,' use the index '3' for the row and index '1' for the column.


1 Answers

A faster way is to use linear indexing directly without calling SUB2IND:

output = A( size(A,1)*(iy-1) + ix )

... think of the matrix A as a 1D array (column-wise order)

like image 157
merv Avatar answered Sep 19 '22 08:09

merv