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?
0 个评论
采纳的回答
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}
3 个评论
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!