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!