Confronting column values of two rows in a table?

1 次查看(过去 30 天)
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?

采纳的回答

Voss
Voss 2021-12-20
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 个评论
Voss
Voss 2021-12-20
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'}

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by