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
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
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
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()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!