-2

Lets assume we have a 3 dimensional matrix A and X_IND = 4:8 and Y_IND = f(X_IND). f is a function like 2*x^2+1. How I can extract following vector out of A:

a = A(X_IND,Y_IND,3)

However, above equation in MATLAB leads to a matrix while the result should be an array since Y_IND is function of X_IND.

sco1
  • 12,154
  • 5
  • 26
  • 48
Roy
  • 65
  • 2
  • 15
  • 40
  • You're indexing `A` as a 3D matrix, so obviously it will output a matrix. Please include the desired result of how `a` should look. – Adriaan Nov 23 '15 at 23:08
  • output = f(X_IND) = A(X_IND, corresponding Y(X_IND)) – Roy Nov 23 '15 at 23:17

1 Answers1

1

Use sub2ind to build a linear index. Here's an example:

>> A = randi(9,2,3,3)
A(:,:,1) =
     6     8     8
     5     2     7
A(:,:,2) =
     8     7     9
     8     7     2
A(:,:,3) =
     8     9     8
     2     4     8

>> X_IND = [1 2]; 
>> Y_IND = X_IND + 1;
>> Z_IND = 3;

>> Z_IND = repmat(3, size(X_IND)); %// all indices should have the same size

>> ind = sub2ind(size(A), X_IND, Y_IND, Z_IND); %// build linear index

>> A(ind)
ans =
     9     8
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147