Filter löschen
Filter löschen

Confronting column values of two rows in a table?

4 Ansichten (letzte 30 Tage)
Fabio
Fabio am 20 Dez. 2021
Kommentiert: Fabio am 20 Dez. 2021
Hello everyone,
I have a table (see attached file) showing specific data (variables as columns) for every element name (rows). I would like to implement a code that allows me to compare between two elements (rows) their respective variable value.
For example, for the giving table, I would like that the code compares the “GWP” variable between element “T” (GWP_T=500) and element “P1” (GWP_P1=400) and it gives me the element name that has a higher “GWP” between the two à “T” (GWP_T=500).
How can I do that?

Akzeptierte Antwort

Voss
Voss am 20 Dez. 2021
t = load('Table.mat');
t = t.Comp;
names = {'T' 'P1'};
[~,idx] = max(t.GWP(ismember(t.Name,names)));
max_name = names{idx}
max_name = 'T'
  3 Kommentare
Voss
Voss am 20 Dez. 2021
A for loop would work, yes. You would just perform the operation specified in my answer for each pair of variable names, i.e., the names variable up there would get each pair of names and the rest would be the same.
t = load('Table.mat');
t = t.Comp;
all_names = {'T' 'P1'; 'T' 'S2'; 'T' 'P2'; 'T' 'S3' }; % I made a slight modification: to use a 4-by-2 cell array here rather than a 1-by-8 but you can do it with a 1-by-8 if you need to
N = size(all_names,1);
max_names = cell(N,1);
for i = 1:N
names = all_names(i,:);
[~,idx] = max(t.GWP(ismember(t.Name,names)));
max_names{i} = names{idx};
end
display(max_names);
max_names = 4×1 cell array
{'T'} {'T'} {'T'} {'T'}
Fabio
Fabio am 20 Dez. 2021
Ok perfect. Thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by