Hamed
Hamed

Reputation: 1213

MATLAB localmax() function returns indexes in 1D vector

In order to calculate local maximums of 2D matrix Y, I use this

[~, indices]= localmax(Y);  

But the indices is 1D. How to convert it back to 2D so to access its corresponding element in Y?

Upvotes: 0

Views: 84

Answers (1)

sco1
sco1

Reputation: 12214

From the documentation for localmax:

Linear indices of the nonzero values of lmaxima. Use ind2sub to convert the linear indices to matrix row and column indices.

For example:

inputmatrix = ...
    [3     2     5     3
    4     6     3     2
    4     4     7     4
    4     6     2     2];


[~,indices] = localmax(inputmatrix,4,false);
[I, J] = ind2sub(size(indices), indices);

Edit: I should have clarified as well. As @LuisMendo mentions in the comments above, you can access the elements of Y directly with these linear indices by using Y(indices).

Upvotes: 3

Related Questions