Counting number of matrix elements with 1 and 2.

2 Ansichten (letzte 30 Tage)
AJA
AJA am 1 Jan. 2021
Kommentiert: AJA am 1 Jan. 2021
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?

Akzeptierte Antwort

Sajjad Asefi
Sajjad Asefi am 1 Jan. 2021
Bearbeitet: Sajjad Asefi am 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

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by