adding element to the beginning of an array

12 views (last 30 days)
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 :)

Accepted Answer

madhan ravi
madhan ravi on 21 Jul 2020
b(1:numel(a)) = a
  7 Comments
madhan ravi
madhan ravi on 22 Jul 2020
I answered your original question.
Ali Abdulkarim
Ali Abdulkarim on 22 Jul 2020
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!