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 :)

 채택된 답변

madhan ravi
madhan ravi 2020년 7월 21일

2 개 추천

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]
madhan ravi
madhan ravi 2020년 7월 22일
Why ? Homework?
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) ?
madhan ravi
madhan ravi 2020년 7월 22일
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에 대해 자세히 알아보기

질문:

2020년 7월 21일

댓글:

2020년 7월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by