MATLAB: How to count number of Elements in a coloumn matrix of 10 entries.

k nearest neighborsknn

Dear I am working on KNN method where a is a resultant classified matrix lets a=[1:1:1:3:1:1:10:3:1:1]. Now I want to write a program that calculates the accuracy of how many 1(ones) are in a. If you do not know KNN method then take it as a simple problem of finding the percentage or weight-age of 1(one) in whole column matrix a. Please any one help me.

Best Answer

  • IIUC, that would simply be
    I'm presuming you meant semicolons in place of the colons in your sample a; for it one gets
    >> a=[1 1 1 3 1 1 10 31 1].';
    >> sum(a==1)/length(a)
    ans =
    BTW, your a has only 9 entries as given...hence the 2/3-rds value of 6/9 instead of an even tenth.