MATLAB Answers

comparing two columns of a table in MATLAB

11 views (last 30 days)
Annick on 27 Mar 2020
Commented: Annick on 27 Mar 2020
I want to compare the second column with the 3rd column to find out if there is any row having the same values in both columns.
Let's say, for example, 1st ,3rd and 4th rows have the same values in both the two columns. I would like to add another column to the table which shows me if the rows are compared with eachother.
Currently I have wrote this loop, but it writes 0 eventhough the columns are the same
if(Table1.Interval1 == Table1.ActivityCode1)
Comparison(i)= 1;
Comparison(i)= 0;
Comparison = (Comparison)';
Could you please help me?


Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Mar 2020
Edited: Geoff Hayes on 27 Mar 2020
Annick - how about you remove the loop and just do
Comparison = Table1.Interval1 == Table1.ActivityCode1;
Wouldn't Comparison be an array of ones and zeros where a one indicates that the two elements (in whatever row) are equal, and a zero indicating that the two elements aren't zero?

  1 Comment

Annick on 27 Mar 2020
Thank you so much for your quick answer :)

Sign in to comment.

More Answers (0)

Sign in to answer this question.