concatenating vectors generated with loop
이전 댓글 표시
I would like to run a for loop like
for i=1:N
output=[1 i]
output2 = [output2 output]
end
But in this example it seems that I need to define output2 at the beginning. Is there any compact way to write this operation?
댓글 수: 1
Stephen23
2018년 4월 2일
Expanding the array on each iteration will (in general) slow down your code. As David Goodmanson wrote, it is good practice preallocate the output array before the loop. Read this to know more:
채택된 답변
추가 답변 (1개)
David Fletcher
2018년 3월 27일
편집: David Fletcher
2018년 3월 27일
Not really more compact, but it does away with the loop
N=10
output=reshape([ones(1,N); 1:N],1,2*N)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!