Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Counting problem

Subject: Counting problem

From: Erasmus

Date: 6 Feb, 2013 21:40:08

Message: 1 of 5

hello everyone, I want count how many times of number meet the specific criteria.
for example, I have two matrix, A and B, I want to count the how many times of element meet two criteria,1) the element in A is less than 1; 2) the corresponding element(in the same location) in B is larger than 50,.

for example

A= [ 0.1 0.2 0.5
      0.9 1.3 1.2
      0.7 1.2 1.5]
B=[2 34 43
     54 55 56
     62 43 45]

so meet the times meeting these two criteria is 2 times, location[3,1] and [2,1]

Does anyone can help me? thank you very much!

Subject: Counting problem

From: dpb

Date: 6 Feb, 2013 22:07:01

Message: 2 of 5

On 2/6/2013 3:40 PM, Erasmus wrote:
> hello everyone, I want count how many times of number meet the specific
> criteria.
> for example, I have two matrix, A and B, I want to count the how many
> times of element meet two criteria,1) the element in A is less than 1;
> 2) the corresponding element(in the same location) in B is larger than 50,.
>
> for example
>
> A= [ 0.1 0.2 0.5
> 0.9 1.3 1.2 0.7 1.2 1.5]
> B=[2 34 43
> 54 55 56
> 62 43 45]
>
> so meet the times meeting these two criteria is 2 times, location[3,1]
> and [2,1]
>

 >> ix=find(A<1);
 >> iab=intersect(ix,find(B(ix)>50));
 >> N=length(iab) % count
N =
      2
 >> [i,j]=ind2sub(size(A),iab);
 >> disp([i j]) % locations
      2 1
      3 1
 >>

--

Subject: Counting problem

From: Jos (10584)

Date: 7 Feb, 2013 11:43:06

Message: 3 of 5

"Erasmus" wrote in message <keuijo$cvu$1@newscl01ah.mathworks.com>...
> hello everyone, I want count how many times of number meet the specific criteria.
> for example, I have two matrix, A and B, I want to count the how many times of element meet two criteria,1) the element in A is less than 1; 2) the corresponding element(in the same location) in B is larger than 50,.
>
> for example
>
> A= [ 0.1 0.2 0.5
> 0.9 1.3 1.2
> 0.7 1.2 1.5]
> B=[2 34 43
> 54 55 56
> 62 43 45]
>
> so meet the times meeting these two criteria is 2 times, location[3,1] and [2,1]
>
> Does anyone can help me? thank you very much!

Create logical matrices using your criteria, like
tfA = A < 1
tfB = B > 50
tfAB = tfA & tfB
N = sum(tfAB(:))

The colon is needed to sum over all elements. All this can be combined in a single line, which is, however, more difficult to understand.

N = sum(A(:)< 1 & B(:)>50)

~ Jos

Subject: Counting problem

From: Steven_Lord

Date: 7 Feb, 2013 14:54:58

Message: 4 of 5



"Jos (10584) " <#10584@fileexchange.com> wrote in message
news:kf040a$35i$1@newscl01ah.mathworks.com...
> "Erasmus" wrote in message <keuijo$cvu$1@newscl01ah.mathworks.com>...
>> hello everyone, I want count how many times of number meet the specific
>> criteria.
>> for example, I have two matrix, A and B, I want to count the how many
>> times of element meet two criteria,1) the element in A is less than 1; 2)
>> the corresponding element(in the same location) in B is larger than 50,.
>>
>> for example
>>
>> A= [ 0.1 0.2 0.5
>> 0.9 1.3 1.2 0.7 1.2 1.5]
>> B=[2 34 43
>> 54 55 56
>> 62 43 45]
>>
>> so meet the times meeting these two criteria is 2 times, location[3,1]
>> and [2,1]
>>
>> Does anyone can help me? thank you very much!
>
> Create logical matrices using your criteria, like tfA = A < 1
> tfB = B > 50
> tfAB = tfA & tfB
> N = sum(tfAB(:))
>
> The colon is needed to sum over all elements. All this can be combined in
> a single line, which is, however, more difficult to understand.
>
> N = sum(A(:)< 1 & B(:)>50)

There's no need for SUM here since you're just looking for a count of the
nonzeros.

tfA = A < 1
tfB = B > 50
tfAB = tfA & tfB
N = nnz(tfAB)

This leads to the one-liner:

N = nnz( (A < 1) & (B > 50) );

The only part of that statement that IMO may be difficult to understand is
the NNZ function, which not a lot of people tend to use or perhaps know
about. [NNZ: it's not just for sparse matrices anymore! ;]

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Counting problem

From: Jos (10584)

Date: 7 Feb, 2013 20:46:08

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <kf0f83$ghh$1@newscl01ah.mathworks.com>...
>
>
> "Jos (10584) " <#10584@fileexchange.com> wrote in message
> news:kf040a$35i$1@newscl01ah.mathworks.com...
*snip
> The only part of that statement that IMO may be difficult to understand is
> the NNZ function, which not a lot of people tend to use or perhaps know
> about. [NNZ: it's not just for sparse matrices anymore! ;]

Thanks Steven!

I am getting old :-(

~ Jos

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us