1
$\begingroup$

I have a matrix like- [1 2 3 3 3;1 2 2 5 5] From this matrix, I want to calculate in matlab that in each row how many numbers of 1 2 3 and 5? The result will be - 1 1 2; 1 2 2

I have tried with numberof1=sum(a(1:ac,:)==1); but this is valid if there is only one row but in my case there are multiple rows so i need to check every rows.

I really need help.

$\endgroup$
1
$\begingroup$

You can use ==1 on the entire matrix A, and then sum along the rows. As in sum(a==1,2); (see sum documentation).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.