2015-10-14 31 views
0

为了计算2D矩阵Y的局部最大值,我使用这个MATLAB localmax()函数返回索引在1D向量

[~, indices]= localmax(Y); 

indices是1D。如何将其转换回2D,以便访问Y中相应的元素?

+1

其实你不需要转换回2D。您可以使用'Y(索引)'(线性索引)来访问这些元素 –

回答

3

the documentation for localmax

lmaxima的非零值的线性索引。使用ind2sub至 将线性索引转换为矩阵行和列索引。

例如:

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); 

编辑:我应该澄清为好。正如@LuisMendo在上面的评论中提到的那样,您可以通过使用Y(indices)直接访问Y的元素与这些linear indices