필터 지우기
필터 지우기

if-else statement for 4 comparison

조회 수: 16 (최근 30일)
Cutie
Cutie 2022년 2월 12일
답변: Cutie 2022년 2월 14일
I want to use if-else & for-loop to make comparison where outputs can be one of four options- TP,TN,FP,FN
Supposed I have two row vectors of whose elements are 1s & 0s.
A= [0;1;0;1;1;0;1];
B= [0;1;1;0;1;1;0];
for i=1:7
if A(i) ==1 && B(i)==1
output TP=1
elseif A(i) ==1 && B(i)==0
output FN=1
elseif A(i) ==0 && B(i)==0
output TN=1
else
output FP=1
end
end
Unrecognized function or variable 'output'.
I need help on how to properly code this. My idea above is not working

채택된 답변

Cutie
Cutie 2022년 2월 14일
Thank you @Cris LaPierre @DGM @David Hill for your help. I have been able to navigate it
A= [0;1;0;1;1;0;1;0;0;1];
B= [0;1;1;0;1;1;0;1;0;1];
TP=zeros(1,size(A,1));
TN=zeros(1,size(A,1));
FP=zeros(1,size(A,1));
FN=zeros(1,size(A,1));
for i=1:7
if A(i) ==1 && B(i)==1
TP(i)=1;
elseif A(i) ==1 && B(i)==0
FN(i)=1;
elseif A(i) ==0 && B(i)==0
TN(i)=1;
else
FP(i)=1;
end
end

추가 답변 (3개)

DGM
DGM 2022년 2월 12일
편집: DGM 2022년 2월 12일
You can do it something like this, assuming your inputs are binarized as described.
A = [0;1;0;1;1;0;1];
B = [0;1;1;0;1;1;0];
strmap = {'TN';'FN';'FP';'TP'};
output = strmap(A + 2*B + 1)
output = 7×1 cell array
{'TN'} {'TP'} {'FP'} {'FN'} {'TP'} {'FP'} {'FN'}

David Hill
David Hill 2022년 2월 12일
편집: David Hill 2022년 2월 12일
A= [0;1;0;1;1;0;1];
B= [0;1;1;0;1;1;0];
output=cell(size(A));
output(A==1&B==1)={'TP'};
output(A==1&B==0)={'FN'};
output(A==0&B==0)={'TN'};
output(A==0&B==1)={'FP'};
  댓글 수: 2
Cutie
Cutie 2022년 2월 12일
@David Hill, thank you for offering your help. However, the vectors can be as much as 50000 rows. So I want it programmed.
DGM
DGM 2022년 2월 12일
It is programmed. David's method uses logical indexing to create one cell array of character vectors which is the same size as A.
A= [0;1;0;1;1;0;1];
B= [0;1;1;0;1;1;0];
output=cell(size(A));
output(A==1&B==1)={'TP'};
output(A==1&B==0)={'FN'};
output(A==0&B==0)={'TN'};
output(A==0&B==1)={'FP'};
output % show the result
output = 7×1 cell array
{'TN'} {'TP'} {'FP'} {'FN'} {'TP'} {'FP'} {'FN'}

댓글을 달려면 로그인하십시오.


Cris LaPierre
Cris LaPierre 2022년 2월 12일
편집: Cris LaPierre 2022년 2월 13일
I modified your code to run it here and display the error.
You are close, but your syntax is incorrect. Assignment in MATLAB is done with the '='. For example
output = 'TN'
Also, each loop will overwrite the previous ouput value with the new one. Once finished, you will only have the result of the final loop. Use your loop index to assign the current output to a specific location in the vector output. For example
output(i) = 'TN'
  댓글 수: 1
Cutie
Cutie 2022년 2월 14일
@Cris LaPierre thank you for the 'tip.' It helped me to write the code I want it. I appreciate your help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by