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

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

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 28 Jul 2015
Edited: Andrei Bobrov on 28 Jul 2015
z = nnz(ismember(B,[1 2],'rows'));

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!