Matlab 多维矩阵的最大值或最小值及其位置索引

%# random 4D array with different size in each dim
A = rand([3,3,3,5]);

%# finds the max of A and its position, when A is viewed as a 1D array
[max_val, position] = max(T(:)); 

%#transform the index in the 1D view to 4 indices, given the size of A
[v,x,y,z] = ind2sub(size(T),position);

举例

求三维矩阵 T 的最大值和最小值及其位置索引

T(:,:,1) = [3 3
            4 2];

T(:,:,2) = [6 8
            9 5];

T(:,:,3) = [16 18
            20 7];       
   
[max_val, position_max] = max(T(:)); 
[x,y,z] = ind2sub(size(T),position_max);

[min_val, position_min] = min(T(:)); 
[r,s,t] = ind2sub(size(T),position_min);

运行结果

max_val = 20

x = 2

y = 1

z = 3


min_val = 2

r = 2

s = 2

t = 1

 

你可能感兴趣的:(Matlab,matlab)