How to find the maximum of some variables ?
6 次查看(过去 30 天)
显示 更早的评论
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
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 个)
Drishan 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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!