Counting number of matrix elements with 1 and 2.

1 次查看(过去 30 天)
Example of my matrix:
A = [10 1;
10 2;
11 1;
11 1;
12 1;
12 1;
12 2;
13 1;
13 1];
The first column is the id code and the second is the details. For example, id 10 has both 1 and 2, so it is counted. Id 11 has only 1s, so it is not counted. Id 12 has both 1 and 2 so it is to be counted. Based on the matrix above, my count shoould be 2. Is there a funtion wherein i can use to count the number of elements with 1 and 2 in a matrix? Or i need to use loop?

采纳的回答

Sajjad Asefi
Sajjad Asefi 2021-1-1
编辑:Sajjad Asefi 2021-1-1
Hi there,
I think you can use the following method to get your answer without writting a "for" loop;
intersect(A(A(:,2)==1,1),A(A(:,2)==2,1))
All the best,
Sajjad

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by