How to make a new column in my table

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

回答(1 个)

Walter Roberson
Walter Roberson 2023-1-25
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 个评论
Walter Roberson
Walter Roberson 2023-1-25
Though in this particular case you could just code
Results(4,:) = Results(2,:) == Results(3,:);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by