How to increase the speed of this code?

4 次查看(过去 30 天)
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

采纳的回答

Roger Stafford
Roger Stafford 2016-4-8
编辑:Roger Stafford 2016-4-8
Try 'ndgrid':
[X,Y,Z] = ndgrid(0.01:0.01:1);
pts = [Z(:),Y(:),X(:)]; %(Corrected)

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-4-8
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(:)]

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by