concatenating vectors generated with loop
Show older comments
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 Comment
Stephen23
on 2 Apr 2018
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:
Accepted Answer
More Answers (1)
David Fletcher
on 27 Mar 2018
Edited: David Fletcher
on 27 Mar 2018
Not really more compact, but it does away with the loop
N=10
output=reshape([ones(1,N); 1:N],1,2*N)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!