How to find the maximum of some variables ?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 19 de Nov. de 2021
Editada: Stephen23
el 19 de Nov. de 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?
Respuestas (1)
Drishan Poovaya
el 22 de Nov. de 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 comentarios
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!