How can I count how many multi-dimension arrays in a matrix?

For example, I have a 10 by 2 matrix 'B' as below.
B=[1 9; 2 9; 4 8; 1 0; 1 0; 1 1; 1 3; 1 3; 1 2; 1 2]
I want to find out how many rows are 1 2 The answer should be 2. I am using for loop as shown below.
z=0;
for f1=1:10
if B(f1,1)==1 && B(f1,2)==2
z = z+1;
end
end
This method works fine but when matrix 'B' gets bigger, it takes too much time. Is there any other ways to do this job?

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2015년 7월 28일
편집: Andrei Bobrov 2015년 7월 28일
z = nnz(ismember(B,[1 2],'rows'));

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

질문:

2015년 7월 28일

댓글:

2015년 7월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by