Thanasis
Thanasis

Reputation: 725

Matlab multiple indexing

I have a matrix like this one:

a= [ 61 65 63 ; ...
     21 21 24 ; ...
     34 2 51 ]
b= [ -1 0 8; ...
      -2 0 6; ...
      -4 0 2]
c=cat(3,a,b)

What I want to do is to find by rows of the first dimension on c the highest value, and then based on these values to get the values at the row and column but in the third dimension of c.

In my example, I am trying the following:

 [maxV,colIndx] =max(c,[],2)

 m=maxV(:,:,1)
 f=c(:,colIndx(:,1,1),2)

The vector m return half of my answer, ie

m =

    65
    24
    51

now, I want f to be like

f =

    0
    6
    2

However, my code doesn't return what I want. Any help is much appreciated!

Upvotes: 2

Views: 410

Answers (1)

Dan
Dan

Reputation: 45752

Try using linear indexing via the sub2ind function:

linIndx = sub2ind(size(c), (1:(size(c,1)))', colIndx(:,:,1), ones(size(c,1),1)*2);
f = c(linIndx)

Upvotes: 4

Related Questions