How to make a new column in my table

3 views (last 30 days)
Sia Sharma
Sia Sharma on 25 Jan 2023
Commented: Walter Roberson on 25 Jan 2023
Hi,
I have the following in my table (Results) of 550 rows:
A = audsActual %in the second row
P = audsPreds %in the third row
I want to make a 4th column that checks if for each row, if A = P, then it says 1. if not, it says 0.
I wrote this code but it is not working:
if Results(2,:) == Results(3,:)
Results (4,:) = 1
elseif Results (4,:) = 0
It's giving me an error message. Please help, thank you!

Answers (1)

Walter Roberson
Walter Roberson on 25 Jan 2023
elseif requires a expression after it, but you coded an assignment.
You just want else not elseif
Remember that
if Results(2,:) == Results(3,:)
is going to test all of row 2 against all of row 3, creating a logical vector of true and false. if and while consider a test to be true only if all of the values being tested are non-zero, so the code you wrote is equivalent to
if all(Results(2,:) == Results(3,:))
which is not what you want.
What you really need to do is read about logical indexing. https://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/
  1 Comment
Walter Roberson
Walter Roberson on 25 Jan 2023
Though in this particular case you could just code
Results(4,:) = Results(2,:) == Results(3,:);

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!