if-else statement for 4 comparison

27 ビュー (過去 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 Center および File ExchangeLoops 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