필터 지우기
필터 지우기

Compare cells of different columns

조회 수: 2 (최근 30일)
Ramiro Rea
Ramiro Rea 2017년 7월 9일
댓글: Ramiro Rea 2017년 7월 9일
Hi, I need to compare the data from columns 1, 2, 3 (where a particular option is located) to the column 4 (what answer was received). If the pair tested matches (e.g. cell 1,1 vs 1,5) I want to output the values located in the same row but in columns 5 and 6. If this is not possible, I just need it to show either a 1 or a 0. For the later I tried the following code:
gmax = [1;2;3;1;2;3];
pmax = [2;3;1;2;3;1];
lmin = [3;1;2;3;1;2];
response = [1;1;2;1;3;2];
start_time = [2.45; 6.32; 8.21; 10.12; 13.09; 16.7];
RT = [2.7433; 2.2333; 3.4532; 2.9919; 3.0011; 2.5509];
T = table (gmax, pmax, lmin, response, start_time, RT);
A = zeros (6,1);
B = zeros (6,1);
C = zeros (6,1);
for j = 1 : 6
A(j,1) = isequal (T(j,1), T(j,5));
end
for j = 1 : 6
B(j,1) = isequal (T(j,2), T(j,5));
end
for j = 1 : 6
C(j,1) = isequal (T(j,3), T(j,5));
end
Can you point me what am I doing wrong? It always returns 0, even if the pair matches. Many thanks

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 9일
편집: Walter Roberson 2017년 7월 9일
gmax = [1;2;3;1;2;3];
pmax = [2;3;1;2;3;1];
lmin = [3;1;2;3;1;2];
response = [1;1;2;1;3;2];
start_time = [2.45; 6.32; 8.21; 10.12; 13.09; 16.7];
RT = [2.7433; 2.2333; 3.4532; 2.9919; 3.0011; 2.5509];
T = table (gmax, pmax, lmin, response, start_time, RT);
A = (T{:,1} == T{:,[4 4]}).*T{1,5:6}
B = (T{:,2} == T{:,[4 4]}).*T{1,5:6}
C = (T{:,3} == T{:,[4 4]}).*T{1,5:6}
... I guess. A sample output would have helped.
Better:
r2 = [response, response];
outs = [start_time, RT];
A = (gmax == r2) .* outs
B = (pmax == r2) .* outs
C = (lmin == r2) .* outs
or
r2 = [T.response, T.response];
outs = [T.start_time, T.RT];
A = (T.gmax == r2) .* outs
B = (T.pmax == r2) .* outs
C = (T.lmin == r2) .* outs
The repetition of the variable being tested against, and the .* by the output columns, is a cheap way of getting the selected information where it exists and "0" where it doesn't.
Watch out with your isequal: you were comparing tables not numeric values.
  댓글 수: 1
Ramiro Rea
Ramiro Rea 2017년 7월 9일
Yes this completely work. I am pretty new with this software, the learning curve is steep. Thank you very much for your help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by