Clear Filters
Clear Filters

Counting number of matrix elements with 1 and 2.

1 view (last 30 days)
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?

Accepted Answer

Sajjad Asefi
Sajjad Asefi on 1 Jan 2021
Edited: Sajjad Asefi on 1 Jan 2021
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!