2016-12-14 66 views
0

變量名我有一個數組Z:發現從陣列MATLAB

A=2; 
B=3; 
C=4; 
D=5; 
E=6; 
Z=[A B C D E]; 

我想找到陣Z的最大值,並且還得到了其具有最大值「變量的名稱」。這個怎麼做?

回答

2

你可以這樣做:

A=2; 
B=3; 
C=4; 
D=5; 
E=6; 
Z=[A B C D E]; 
x = ['A' 'B' 'C' 'D' 'E']; 
[maximum,idx] = max(Z); 
disp(['maximum is :' num2str(maximum)]); 
disp(['variable name is :' x(idx)]); 
0

另一種可能的解決方案:

ZNames = {'A','B','C','D','E'} 
biggestVar = ZNames(find(Z==max(Z),1,'first')) 

結果

biggestVar = 'E'