필터 지우기
필터 지우기

Determine how many times numbers in a certain range were drawn

조회 수: 1 (최근 30일)
Rich
Rich 2013년 7월 23일
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