Reputation: 427
I have a simple question about how can I copy a vector into another. I have a vector with a length of 66x1 and then, another with a length of 2151x1. I want to copy the values from the first one in a exactly position on the other. I've tried that but it doesn't work.
inter= 66x1 out= 2151x1
for i=1:numel(inter)
out(101:167)= inter(i)
end
Also I've tried this:
for inter=(1:66);
out(101:167)=inter;
end
And this:
for k= (101:167)
out(k)=inter(1:66);
end
Am I doing wrong? Thanks in advance,
Upvotes: 0
Views: 5386
Reputation: 3640
Let's say your vectors are
a = [1; 2; 3];
b = [4; 5; 6; 7; 8; 9];
for simplicity.
There is no need to use loops. You can just go ahead and do it like this:
startIdx = 2; %101 in your case
finalIdx = startIdx + size(a,1) - 1; % 166 in your case
b(startIdx:finalIdx) = a;
Then b would be:
b =
4
1
2
3
8
9
A very important point here is the -1
in finalIdx
. You need to substract 1 from the final index.
Upvotes: 6