Azzera filtri
Azzera filtri

How to find the maximum of some variables ?

2 visualizzazioni (ultimi 30 giorni)
Ben Worrall
Ben Worrall il 19 Nov 2021
Risposto: Drishan Poovaya il 22 Nov 2021
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 Commento
Stephen23
Stephen23 il 19 Nov 2021
Modificato: Stephen23 il 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?

Accedi per commentare.

Risposte (1)

Drishan Poovaya
Drishan Poovaya il 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

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by