Compare cells of different columns

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 9 Jul 2017
Edited: Walter Roberson on 9 Jul 2017
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 Comment
Ramiro Rea
Ramiro Rea on 9 Jul 2017
Yes this completely work. I am pretty new with this software, the learning curve is steep. Thank you very much for your help!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!