필터 지우기
필터 지우기

Construct a vector from another, but add 2 values every 10 iteration

조회 수: 1 (최근 30일)
V.D-C
V.D-C 2020년 1월 11일
댓글: V.D-C 2020년 1월 11일
Hello !
I would like to create a vector B from a vector A, and at every 10th iteration, implement 2 times the same value from A:
A = linspace(0,1,811);
B = [];
for i = 1:1:length(A)
if mod(i,10) ~= 0
B = [B,A(1,i)];
elseif mod(i,10) == 0
added_value = A(1,i);
B = [B,added_value,added_value];
end
end
I know the length of the vector B should be around 901. I should have 90 "added_values", but when I verify it I have almost 1000 of them.
And it seems at every iteration the "if" condition just skips to the "elseif" part... What should I do to make this work ? I can't think of any other method...
Thanks in advance !

채택된 답변

Meg Noah
Meg Noah 2020년 1월 11일
Try this:
npts = 811;
A = linspace(0,1,npts);
numAdded = sum(mod(1:npts,10) == 0);
B = zeros(1,numAdded+npts);
k = 0;
for i = 1:1:npts
k = k + 1;
B(k) = A(1,i);
if mod(i,10) == 0
k = k + 1;
B(k) = A(1,i);
end
end
% another way to do it...
npts = 811;
A = linspace(0,1,npts);
idx = 1:npts;
idxRepeat = find(mod(1:npts,10) == 0);
idxA2B = sort([idx idxRepeat]);
B = A(idxA2B);
  댓글 수: 1
V.D-C
V.D-C 2020년 1월 11일
You just solved 2 days of intense failures, I can't thank you enough !!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by