Return the indices of max value in a possible 4D array

4 ビュー (過去 30 日間)
Samuel Mustapha
Samuel Mustapha 2015 年 2 月 20 日
コメント済み: Samuel Mustapha 2015 年 2 月 20 日
I'm trying to get the indices of a max value from a 4 dimension array, that can actually reduce in dimension, so I can display this value and some the value of some other variables at one or more of the same indices. I used something like this:
MAX = find(sx == max(sx(:)));
if MAX/(kk*qq*zz) > 1
rmax = floor(MAX/(kk*qq*zz)) + 1;
MAX = MAX - (kk*qq*zz) * (rmax - 1);
else
if length(theta) == 1
rmax = 0;
rmax = 1;
end
if MAX/(kk*qq) > 1
zmax = floor(MAX/(kk*qq)) + 1;
MAX = MAX - (kk*qq) * (zmax - 1);
else
zmax = 1;
end
if MAX/kk > 1
qmax = floor(MAX/kk) + 1;
MAX = MAX - (kk) * (qmax - 1);
else
qmax = 1;
end
kmax = MAX;
smax = sx(kmax, qmax, zmax, rmax);
but if the array size changes it still wants to output smax as a 4 dimensional array except its not so I get an error.
Is there any function in Matlab for getting these indices?

採用された回答

Andrew Newell
Andrew Newell 2015 年 2 月 20 日
編集済み: Andrew Newell 2015 年 2 月 20 日
If you don't know in advance the size of the array, you can create a cell array for the indices with as many elements as there are dimensions:
[MAX,I] = max(sx(:));
dims = size(sx);
smax = cell(size(dims));
[smax{:}] = ind2sub(dims,I);
  3 件のコメント
Andrew Newell
Andrew Newell 2015 年 2 月 20 日
That would be more transparent. My code is rather cryptic.
Samuel Mustapha
Samuel Mustapha 2015 年 2 月 20 日
Thank you so much. Ind2sub worked on its own. It's a varargout function and it has an if statement for putting ones in empty dimensions. Since I know the max dimension I'll have, it worked. Thanks again.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by