How can plot every n-th element in a vector AND the last element?

Hello everyone,
i want to plot just every n-th vector element in a for-loop, to speed up the plot loading time.
But I need also the last element to be plotted.
For example:
%p = [1,2,3,...,100,102,102]
n = 10
for i=10:n:102
k(i) = p(i)
...
end
The vector should than look like this: [10,20,30,40,50,60,70,80,90,100,102].
Also it would be great if i could avoid the error because of index exceeding the array bounds.
Thank you in advance!

 채택된 답변

James Tursa
James Tursa 2020년 3월 30일
편집: James Tursa 2020년 3월 30일
If you don't care about possibly overplotting that last point twice, then simply
n = 10;
result = [p(n:n:end) p(end)];
If you want to make sure that last point doesn't overplot, then add some logic to avoid it. E.g.,
if( mod(numel(p),n) )
result = [p(n:n:end) p(end)];
else
result = p(n:n:end);
end

댓글 수: 1

First of all, thanks for your answer!
Manipulating my result vector like this would take a good logic to be stable.
Do you know any other way to boost the plot speed, without manipulating the result vector?

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2020년 3월 30일

댓글:

2020년 3월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by