Clear Filters
Clear Filters

Determine how many times numbers in a certain range were drawn

1 view (last 30 days)
Hi All I have a 52,1 matrix populated with random matrix.
I want to be able to count the sum of a range, for example I want to be able to sum the values between the (3,1) and (6,1)
I tried using the code range=sum(matrixname(lowvalue:highvalue))
but that does not work
Any suggestions are appreciated
Thanks!
Rich
  3 Comments
Evan
Evan on 23 Jul 2013
When you say "that does not work" what do you mean? Do you get an error? Is this value returned simply wrong?
Image Analyst
Image Analyst on 24 Jul 2013
And what does it mean to count the sum? The sum is the total of the numbers. What is the "count" of that?

Sign in to comment.

Answers (1)

dpb
dpb on 23 Jul 2013
On the assumption the (3,1) refers to the subscript of the array (and, btw, the "1" is superfluous for the 1D dimension for vectors, use just "(3)" instead),
>> x=rand(52,1);
>> x(2:7)'
ans =
0.7138 0.8844 0.7209 0.0186 0.6748 0.4385
>> sum(x(3:6))
ans =
2.2986
>> nlow=3;nhi=6;
>> sum(x(nlow:nhi))
ans =
2.2986
>>
If, instead you mean the between the values stored at those locations, then use logical addressing...since don't know the magnitudes of the values at those locations, to do logic on them will need to find the larger/smaller first...
>> xlo=min(x(nlow),x(nhi))
xlo =
0.6748
>> xhi=max(x(nlow),x(nhi))
xhi =
0.8844
>> (x(x>=xlo & x<=xhi))
ans =
0.7138
0.8844
0.7209
0.6748
0.8147
0.7223
0.8319
0.8593
0.8266
0.8186
>> sum(x(x>=xlo & x<=xhi))
ans =
7.8673
>>
Of course, your answers will be different depending on the values returned by rand()
  5 Comments
Rich
Rich on 25 Jul 2013
Here is what I am trying to do, maybe this will help understand my code
I am trying to generate 2500 random numbers between 1 and 52, and I am trying to count how many times each is selected
So I generate an array with the 2500 random numbers then I round them then I turn them into another array with the values being how many times they were selected then I let the user pick a range to count how many times the numbers in the range were picked.
Thanks again!

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!