Get max value and index of multidimensional array

206 ビュー (過去 30 日間)
Evan
Evan 2013 年 2 月 13 日
コメント済み: Steven Lord 2022 年 1 月 12 日
hello
i have an output value matrix depending on 3 inputs ranging from p10 to p11 , p20 to p21, p30 to p31. the output matrix is multidimensional depending on number of inputs. i want to write a function that gives the max value and its coordinates so to speak of the max value. matlabs max function only does 1 dimension.
any help would be grateful. thank you

回答 (4 件)

ChristianW
ChristianW 2013 年 2 月 13 日
Use 1D-Indexing:
M = randn(10,10,10,10);
[C,I] = max(M(:));
C
M(I)
This might be useful too:
[I1,I2,I3,I4] = ind2sub(size(M),I);
M(I1,I2,I3,I4)
  2 件のコメント
Evan
Evan 2013 年 2 月 13 日
The second part is exactly what I needed. Absolutely brilliant.
Thanks!
Nathan White
Nathan White 2017 年 5 月 25 日
I tried using this myself, but I'm not understanding how to do it. I have a 10 x 10 x 10 matrix and I need to find which row, column and page gives the max value. Could you clarify a little more what you did here? Thanks.

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


David McMahon
David McMahon 2022 年 1 月 11 日
編集済み: David McMahon 2022 年 1 月 11 日
Using more modern versions of Matlab, I found a simple solution that works for any size of matrix:
In the case of, say, 3D:
[M,I] = max(A,[],"all","linear");
[dim1, dim2, dim3] = ind2sub(size(A),I)
  1 件のコメント
Steven Lord
Steven Lord 2022 年 1 月 12 日
To generalize the last line:
% Sample data
nd = randi([2 10]); % random integer between 2 and 10
sz = randi(4, 1, nd) % vector of nd integers representing size(A)
sz = 1×7
4 1 2 3 2 3 3
A = randi([-10 10], sz); % random array
% Your first line
[maxValue, maxInd] = max(A, [], "all", "linear")
maxValue = 10
maxInd = 58
% Allocate a cell array to contain the subscript indices
d = cell(1, ndims(A));
% Use the cell array as a comma-separated list to hold the indices
[d{:}] = ind2sub(size(A), maxInd)
d = 1×7 cell array
{[2]} {[1]} {[1]} {[2]} {[1]} {[2]} {[1]}
% Convert the subscript indices back into a linear index
checkInd = sub2ind(size(A), d{:})
s = 58
% Use the subscript indices to get the element from A
checkValue = A(d{:})
checkValue = 10

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


Youssef  Khmou
Youssef Khmou 2013 年 2 月 13 日
編集済み: Youssef Khmou 2013 年 2 月 13 日
Hi, try this function
function [Max,index] =Max3d(x)
N=size(x);
if length(N)<3
error(' Input 3D array');
end
[a,t]=max(x(:));
Max=a;
index1=ceil(t/(N(1)*N(2)));
%Now we find the slice that contains the Max
Temp=x(:,:,index1);
[index2,index3]=find(Temp==max(Temp(:)));
index=[index2;index3;index1]';
  6 件のコメント
Youssef  Khmou
Youssef Khmou 2013 年 2 月 13 日
ChrsitanW : please try the function and see if is it the same approach/ answer you gave or not .
Of course it also uses max(G(:)) but from there, you can find z coor, then x and y using built-in functions "ceil" and "find" .
ChristianW
ChristianW 2013 年 2 月 13 日
Eheh, yeah its the same, but ind2sub is a validated function working for all dimensions. But its fine, your approach can be useful for someone.

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


Anxo Tato
Anxo Tato 2018 年 2 月 12 日
編集済み: Anxo Tato 2018 年 2 月 12 日
For a 2-D matrix this code can help. It calculates the indices (row and column) of the maximum (value) of a matrix A. Attention: It doesn't deal with multiple maximums!
function [row,column,value] = max_matrix(A)
[y,in] = max(A);
[value,column] = max(y);
[~,row] = max(A(:,column));
end
  1 件のコメント
Mohammad Torabi
Mohammad Torabi 2021 年 7 月 27 日
編集済み: Mohammad Torabi 2021 年 7 月 27 日
Good, I found an easier way to get column:
[value,column] = max(max(A));
[~,row] = max(A(:,column));

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by