how to find maximum or minimum element in a matrix?
1 次查看(过去 30 天)
显示 更早的评论
example
a=5 b=8 c=2 d=6
x=[a b c d]
how to find minimum or maximum element of matrix x? the answer i want is its variable not its value. so the answer here is b and not 8
0 个评论
采纳的回答
Azzi Abdelmalek
2012-10-24
编辑:Azzi Abdelmalek
2012-10-24
a=5;b=8;c=2;d=6
x=[a b c d];
y={'a','b','c','d'};
[c1,idx1]=max(x);
[c2,idx2]=min(x);
max_x=y{idx1}
minx_x=y{idx2}
0 个评论
更多回答(2 个)
Walter Roberson
2012-10-24
function varname = max_of_vars(varargin)
[val, idx] = max([varargin{:}]);
varname = inputname(idx);
end
function varname = min_of_vars(varargin)
[val, idx] = min([varargin{:}]);
varname = inputname(idx);
end
Example:
a=5;b=8;c=2;d=6
max_of_vars(a, b, c, d)
min_of_vars(a, b, c, d)
This code is generalized to any number of variables, provided that each of them is scalar. If an expression is passed instead of a variable and that expression happens to be the maximum, then the empty string '' will be returned.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!