0

I stumbled upon some matlab code where a 1D vector (say "signal" with length 100) is indexed by a 3D matrix (say "distances" with dimensions 10x10x10) and the result ("signal(distances)") is a 3D matrix with exact same dimensions 10x10x10 as the 3D matrix indexed by.

What does matlab actually do there? Where did the vector's elements go? Do you know keywords to search for?

I searched stackoverflow and the mathworks help pages with keywords like "indexing" and so on, but I didn't find anything useful...

Thank you!

Eitan T
  • 32,660
  • 14
  • 72
  • 109
BQ92-PwDD3
  • 13
  • 3

1 Answers1

3

In another answer of mine, I explain in the "Linear indexing with matrix subscripts" part that is also possible to use another matrix for indexing.

The subscript matrix is simply converted into a column vector, and used for linear indexing. The resulting matrix is, however, always of the same dimensions as the subscript matrix.

For instance, suppose that:

A = [10 20 30 40 50 60 70 80 90];
I = [1 3; 1 2];

In such case A(I) is the same as writing reshape(A(I(:)), size(I)). The result is:

ans = 
    10    70
    10    40
Community
  • 1
  • 1
Eitan T
  • 32,660
  • 14
  • 72
  • 109