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);

%内容摘自
%https://www.it1352.com/904306.html
%https://www.ilovematlab.cn/thread-78168-1-1.html

举例

求三维矩阵 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

 

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页