user5916581
user5916581

Reputation: 87

finding variable name from array matlab

I have an array Z:

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

I want to find the max value of Array Z and also get the 'name of the variable' that has that max value. How to do this?

Upvotes: 0

Views: 91

Answers (2)

ibezito
ibezito

Reputation: 5822

Another possible solution:

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

Result

biggestVar = 'E'

Upvotes: 1

Maziyar Gerami
Maziyar Gerami

Reputation: 313

You can do this:

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

Upvotes: 3

Related Questions