필터 지우기
필터 지우기

Confronting column values of two rows in a table?

조회 수: 2 (최근 30일)
Fabio
Fabio 2021년 12월 20일
댓글: Fabio 2021년 12월 20일
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'}
Fabio
Fabio 2021년 12월 20일
Ok perfect. Thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (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