필터 지우기
필터 지우기

Counting number of matrix elements with 1 and 2.

조회 수: 1 (최근 30일)
AJA
AJA 2021년 1월 1일
댓글: AJA 2021년 1월 1일
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