How to increase the speed of this code?

1 view (last 30 days)
Here, I want to generate a 3D lattice points in the form of 3 column tuples. Here is my code:
% code
pts = [];
for i = 0.01:0.01:1
for j = 0.01:0.01:1
for k = 0.01:0.01:1
a = [i j k];
pts = [pts;a];
end
end
end
end
It is too slow when I run this script in the Matlab, is there any way to increase the speed of the operation? Or is there any way to decrease the number of the for loop?
Thanks

Accepted Answer

Roger Stafford
Roger Stafford on 8 Apr 2016
Edited: Roger Stafford on 8 Apr 2016
Try 'ndgrid':
[X,Y,Z] = ndgrid(0.01:0.01:1);
pts = [Z(:),Y(:),X(:)]; %(Corrected)

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 8 Apr 2016
i = 0.01:0.01:0.1
j = 0.01:0.01:.1
k = 0.01:0.01:.1
[ii,jj,kk]=meshgrid(i,j,k)
out=[kk(:) ii(:) jj(:)]
  1 Comment
Roger Stafford
Roger Stafford on 9 Apr 2016
@Azzi: I think these will not be in the same order requested by Zhongruo.

Sign in to comment.

Categories

Find more on Function Creation 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!