Determine how many times numbers in a certain range were drawn

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 commentaires

What is the sum of range?
When you say "that does not work" what do you mean? Do you get an error? Is this value returned simply wrong?
And what does it mean to count the sum? The sum is the total of the numbers. What is the "count" of that?

Connectez-vous pour commenter.

Réponses (1)

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 commentaires

Hi, Thanks for the reply
I tried using the exact code you posted and this is the error I receive Subscript indices must either be real positive integers or logicals.
Error in DELETE_ME (line 5) sum(x(3:6))
everything up to the sum works
dpb
dpb le 24 Juil 2013
Modifié(e) : dpb le 24 Juil 2013
Show your work... something's not the exactly the same.
Okay so you were right, your code does work. IT has something to do with my program being ran before it. I put a clc and clear at the beginning of the dummy program and it works just fine. Now my problem is I dont understand why my program doesnt work, it appears to be the same as yours.
Please help, I am missing something
results=zeros(2500,1);
for i=1:1:2500
results(i,1)=1+51*rand(1);
end
roundedresults=round(results);
newresults=zeros(52,1);
for i=1:1:52
sum=0;
for j=1:1:2500
if roundedresults(j,1)==i
sum=sum+1;
newresults(i,1)=sum;
else
end
end
end
lowrange=input('Input the value for the low end of the range: ');
highrange=input('Input the value for the high end of the range: ');
range=sum(newresults(lowrange:highrange))
*The error I receive is
Index exceeds matrix dimensions.
Error in filename (line 75) range=sum(newresults(lowrange:highrange))*
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!
doc histc

Connectez-vous pour commenter.

Tags

Question posée :

le 23 Juil 2013

Community Treasure Hunt

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

Start Hunting!

Translated by