Can these two for loops be avoided?
Show older comments
I'm currently stuck with this double for loop. Any way I can avoid this?
points = zeros(1000000,2);
counter = 1;
for i=1:1000
for j=501:1500
points(counter,:) = [i j];
counter = counter + 1;
end
end
Accepted Answer
More Answers (1)
Image Analyst
on 5 Nov 2012
points(1:1000, 1) = 1:1000;
points(1:1000, 2) = 501:5000;
2 Comments
Henrik
on 5 Nov 2012
Image Analyst
on 5 Nov 2012
Edited: Image Analyst
on 5 Nov 2012
You can do it with repmat() and imresize() (if you have the Image Processing Toolbox).
numberOfRows = 10000;
column1 = imresize((1:1000)', [numberOfRows 1], 'nearest')
column2 = repmat((1:10)', [numberOfRows/10 1])
points = [column1, column2]
Categories
Find more on Loops and Conditional Statements 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!