0
爲了計算2D矩陣Y的局部最大值,我使用這個MATLAB localmax()函數返回索引在1D向量
[~, indices]= localmax(Y);
但indices
是1D。如何將其轉換回2D,以便訪問Y中相應的元素?
爲了計算2D矩陣Y的局部最大值,我使用這個MATLAB localmax()函數返回索引在1D向量
[~, indices]= localmax(Y);
但indices
是1D。如何將其轉換回2D,以便訪問Y中相應的元素?
從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。
其實你不需要轉換回2D。您可以使用'Y(索引)'(線性索引)來訪問這些元素 –