adding element to the beginning of an array

I have a array which has been initialized with zeros, I would like to add four elements each time at the beginning of it while keeping its size the same,
I want to add the first four elements from a array to the first four elements to a second array(initialized with zeros) and then the next four till we have all the elements from the first one transfer to the second with the order that I have mentioned perviously and with the same manner.
I think it can be done without using loops, I would like to see the best way that Matlab offers to do this.
for example I have:
a = randi(10,1000,1);
% I want to add the first for elements from a to the beginning of b while keeping the size of b the same, and then the next four from a
% to be and so on..
b = zeros(2048,1);
Any help would be really appreciated :)

 采纳的回答

b(1:numel(a)) = a

7 个评论

Thank you for answering, but how about doing it on stages, I mean at first 4 elements, then another four after them, till they reach 1000 ?
I mean
a = [1,2,3,4,5,6,7,8,9,10,11,12,13]
b = [0,0,0,0,0,0,0,0,0,0]
%%% first stage:
b = [1,2,3,4,0,0,0,0]
%%% second stage:
b = [1,2,3,4,5,6,7,8,0,0]
Haha no, actually I am trying to self-learn a certain technology in telecommunications, and I need to have it as stages, becuase that's what real world transmission actually work, you send symbols, and each symbol carry a certain number of bits
w = 1:8;
x = 1:4:numel(w);
y = 4:4:numel(w);
for k = 1:numel(w)/4
w(x(k) : y(k))
end
Thanks, that is what I am looking for, is there a way in matlab to do it using inner loops, instead of for loop?
the output as ans is correct, how about appending the result to the zero initialized vedctor, instead of printing it, shall I use concatenation (cat) ?
I answered your original question.
In my original question, I have mentioned this, In other words I am not trying to extend the question, I have already accepted your answer, becuase It was helpful, thanks anyways

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by