Struggling with for loop if and else to set up a confusion matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi there, I have an array of 2 by 100 called Finalprobs, the first row contains results for healthy people and the second row contains results for unhealthy people. I want to set up a test to see if it is possible to differentiate between the healthy and the unhealthy patients by forming a confusion matrix.
I have set a value for K (just as an example here of 1) and if the value for unhealthy patients in the second row of Finalprobs is lower than K I want to add one to a count of TP (true positive), if the value is higher I want to add one to FN(false negative). I want to do this 100 time for all values of unhealthy patients.
For the healthy patients on the first row of Finalprobs I then want to test but here if their value is lower than K one should be added to FP(false positive), if the value is higher here I want to add one to TN(true negative). Again I want to do this 100 times for all values for healthy patients.
At the end I should therefore have 200 counts in total across the 4 parts of the confusion matrix.
Below is the code that I have tried so far by trying to piece together other bits of code from other more simple loops I have used previously, but I am a bit out of my depth here I think and have commented in what I am wanting to do but not sure if the code I have written is close to performing the job.
TP=0
TN=0
FP=0
FN=0 %set variables for the confusion matrix
K = 1 %set K to 1 for testing purposes
for confuseloopAF = 1:100 %start of for loop for unhealthy patients
if (Finalprobs(2,:)) < K %if unhealthy patients value is less
TP=TP+1; %than K add one to count of TP
else
if (Finalprobs(2,:)) > K %if unhealthy patients value is more
FN=FN+1; % than K add one to count of FN
else
confuseloopAF; %run loop again until end
end;
for confuseloopCO = 1:100 %start of loop for healthy patients
if (Finalprobs(1,:)) < K %if healthy patients value is less
FP=FP+1; %than K add one to count of FP
else
if (Finalprobs(1,:)) < K % if healthy patients value is more
TN=TN+1; % than K add one to count of TN
else
confuseloopCO; %run loop again until end
end;
You will see that I have broken it down into two separate loops as I was really struggling to get my head around putting it all into 1 loop although I know it is possible. It would probably be better in one loop but I was just trying to break it down to as simple as possible in my code to try to get it to work somehow.
댓글 수: 0
채택된 답변
Jan
2017년 3월 14일
편집: Jan
2017년 3월 14일
The statement "confuseloopAF; %run loop again until end" is confusing only. Simply omit it.
Your loops miss a trailing end command. And the loop counter confuseloopAF is not used anywhere. This looks like the for loop is not clear yet. Please read: doc for.
The code does not need a loop at all:
TP = sum(Finalprobs(2,:) < K)
FN = sum(Finalprobs(2,:) > K)
FP = sum(Finalprobs(1,:) < K)
TN = sum(Finalprobs(1,:) < K) % Same as FP?!
Instead of sum the nnz can count the number of non-zero elements also.
댓글 수: 6
Jan
2017년 3월 14일
You are welcome. This was an interesting example to compare a simple loop and the smart vectorization of Matlab. Both have advantages.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!