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!
댓글 수: 0
답변 (1개)
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
2023년 1월 25일
Though in this particular case you could just code
Results(4,:) = Results(2,:) == Results(3,:);
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!