How to find the maximum of some variables ?

조회 수: 6 (최근 30일)
Ben Worrall
Ben Worrall 2021년 11월 19일
답변: Alamanda Ponappa Poovaya 2021년 11월 22일
I have some variables, variable1 = a , variable2 = b , variable3 = c etc.
I want to find the maximum of all the variables and display the variable name not its value, how is this done ?
  댓글 수: 1
Stephen23
Stephen23 2021년 11월 19일
편집: Stephen23 2021년 11월 19일
MATLAB is designed to work efficiently with vectors and arrays. Your task would be trivially easy with a vector/matrix.
"I want to find the maximum of all the variables ..."
Those data are very poorly designed for this otherwise very simple task.
"and display the variable name not its value, how is this done ?"
This implies that whoever created that data made the mistake of putting some meta-data into the variable names. Putting meta-data (e.g. patient names, dates/times, pseudo-indices) into variable names makes accessing that data (and meta-data) slow, complex, and inefficient:
You have not told us the most important information: how did you get all of those variables into the MATLAB workspace?

댓글을 달려면 로그인하십시오.

답변 (1개)

Alamanda Ponappa Poovaya
Alamanda Ponappa Poovaya 2021년 11월 22일
You can refer to the function below, it solves your issue
function dispwithname(varargin)
[~, idx] = max([varargin{:}]);
disp(['Maximum : ' inputname(idx) ' = ' num2str(varargin{idx})])
end
Assume you have 4 variables, you can call the function as seen below
a = 4;
b = 5;
c = 6;
d = 7;
dispwithname(a,b,c,d)
%output -> "Maximum : d = 7"
Feel free to change the output string
Just a nore, refer to the documentation for inputname below.
Thus function only works when inside another function

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by