필터 지우기
필터 지우기

how to find maximum or minimum element in a matrix?

조회 수: 3 (최근 30일)
eri
eri 2012년 10월 24일
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

채택된 답변

Azzi Abdelmalek
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}

추가 답변 (2개)

Walter Roberson
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.

Sachin Ganjare
Sachin Ganjare 2012년 10월 24일
you can use min() & max() functions

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by