Max()-function and multiple dimensions

3 ビュー (過去 30 日間)
Mauri
Mauri 2014 年 8 月 9 日
コメント済み: Steven Lord 2016 年 10 月 25 日
Hello,
I'm trying to maximize a matrix that is of several dimensions. I also need to know the locations of the maximized value, which is actually the problem. How do I go about doing this in the most simple way?
So, for example, x=rand(10,10,10,10,10); Of course, by simple max(max(max(max(x) I could get the maximal value of x. How about the location? (for example the maximum could be located in x(1,2,3,2,4))
Cheers,
M
  1 件のコメント
Image Analyst
Image Analyst 2014 年 8 月 10 日
I think you're getting only answers on how to locate the max value because no one knows what you want to do to maximize the array. What is the array dependent on such that changing those parameters will maximize the array? And what does it mean to maximize an array, as opposed to a single value. Do you want to vary some parameter, say lambda or whatever you want to call it, such that the sum, or mean, of all the values in the array is maximized?

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

採用された回答

Ahmet Cecen
Ahmet Cecen 2014 年 8 月 9 日
編集済み: Ahmet Cecen 2014 年 8 月 9 日
Do:
[C I]=max(x(:));
now C is the maximum value, I is the index of where it is, so that x(I) is C.
If you want the 5D coordinates explicitly, then read ind2sub.
  3 件のコメント
Shivanand Venkanna Sheshappanavar
Shivanand Venkanna Sheshappanavar 2016 年 10 月 25 日
what if the matrix is a 2D matrix ? We need maxval, rowindex and columnindex
Steven Lord
Steven Lord 2016 年 10 月 25 日
The solution Ahmet and Jan posted works fine for 2-D matrices, 5-D arrays, etc.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 9 日
編集済み: Azzi Abdelmalek 2014 年 8 月 10 日
Edit
A=rand(10,10,10,10,10);
[max_value,idx]=max(A(:));
q=['[' sprintf('x%d,',1:ndims(A))];
q(end)=']';
eval([q '=ind2sub(size(A),idx)'])
eval(['x=' q])
  1 件のコメント
Jan
Jan 2014 年 8 月 10 日
編集済み: Jan 2014 年 8 月 10 日
This is cruel. There is no reason to go the indirection over the evil eval. eval is prone to bugs, hard to debug, disables the JIT acceleration and is a severe drawback for efficient programming.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by