About Preallocating for speed
1 view (last 30 days)
Show older comments
Hari on 14 Feb 2017
Answered: Alexandra Harkai on 14 Feb 2017
What is a more efficient way of writing this code?
NumberedOD = ;
for a = 1:(N-1)
for b = (a+1) :N
NumberedOD = [NumberedOD;a, b];
My aim is to get a list of numbers like this:
Adam on 14 Feb 2017
Edited: Adam on 14 Feb 2017
Purely from a pre-allocation perspective you can work out the number of elements there will be in that matrix - it is fixed and calculable so you can preallocated a zeros matrix. I'm sure there is a quicker vectorized way to do it though, I just don't have time to think about it myself right now. I'm sure someone will suggest one though.
More Answers (0)
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!