Find the length of a vector up to a certain value

조회 수: 9 (최근 30일)
Holmbrero
Holmbrero 2022년 6월 7일
댓글: Holmbrero 2022년 6월 8일
Hi!
I have a vector (SortN1250) which is 75558x1 with a range from 1:1000 and i want to extract the length of the vector at certain values (100:100:5000) and ad them to another vector. This is my code:
ProbdistPoredens(50,1)=zeros
for n=100:100:5000
ind = find(abs(SortB1250(:,1)<n));
lengthind=length(ind)
for i=1:50
ProbdistPoredens(i,1)=lengthind
end
end
ProbdistPoredens is however just filled with the number 75558 on all 50 rows. This number is only expeted to show at the very last row in the column.
Any suggestions would be greatly appriciated!
Regards,
  댓글 수: 2
Torsten
Torsten 2022년 6월 7일
length(ind) is just one value (in your case 75558), not an array.
Holmbrero
Holmbrero 2022년 6월 7일
But ind should change with n, right?

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2022년 6월 7일
Explain in words what this means: (100:100:5000)
Do you want only elements that have values of 100, 200, 300, 400, ..., 4900, or 5000?
Is that what "at certain values" means? Or does it mean at certain indexes?
  댓글 수: 3
Image Analyst
Image Analyst 2022년 6월 8일
Try this:
% Create sample data.
SortB1250 = 1000 * rand(75558, 1);
ProbdistPoredens=zeros(50,1);
n = 100 : 100 : 5000
n = 1×50
100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000
for k = 1 : length(n)
ind = find(SortB1250 < n(k));
ProbdistPoredens(k) = length(ind);
end
plot(ProbdistPoredens, 'b.-', 'lineWidth', 2)
grid on;
Holmbrero
Holmbrero 2022년 6월 8일
Works great, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
Simon Chan 2022년 6월 8일
The inner for loop is not necessary and it overwrite itself for every 'n'.
You may modify the code as follows.
ProbdistPoredens(50,1)=zeros;
for n=100:100:5000
ind = find(abs(SortB1250(:,1)<n));
lengthind=length(ind);
ProbdistPoredens(n/100,1)=lengthind; % modify the index from i to n/100
end
Alternatively, you can use function histcounts to do the same thing.
[ProbdistPoredens,~] = histcounts(SortB1250,0:100:5000,'Normalization','cumcount') % Use function hiscounts

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by