Compare cells of different columns

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