Determine how many times numbers in a certain range were drawn

1 次查看(过去 30 天)
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 个评论
Evan
Evan 2013-7-23
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 2013-7-24
And what does it mean to count the sum? The sum is the total of the numbers. What is the "count" of that?

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2013-7-23
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 个评论
Rich
Rich 2013-7-25
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by