Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Counting problem
Replies: 4   Last Post: Feb 7, 2013 3:46 PM

 Messages: [ Previous | Next ]
 Jos Posts: 1,268 Registered: 10/24/08
Re: Counting problem
Posted: Feb 7, 2013 6:43 AM

"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

Date Subject Author
2/6/13 dpb
2/7/13 Jos
2/7/13 Steven Lord
2/7/13 Jos
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}