Counting number of matrix elements with 1 and 2.

2 visualizaciones (últimos 30 días)
AJA
AJA el 1 de En. de 2021
Comentada: AJA el 1 de En. de 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?

Respuesta aceptada

Sajjad Asefi
Sajjad Asefi el 1 de En. de 2021
Editada: Sajjad Asefi el 1 de En. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by