How to find the maximum of some variables ?
6 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 19 Nov 2021
Edited: Stephen23
on 19 Nov 2021
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?
Answers (1)
Drishan Poovaya
on 22 Nov 2021
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
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!