How to increase the speed of this code?

조회 수: 2 (최근 30일)
Zhongruo Wang
Zhongruo Wang 2016년 4월 8일
댓글: Roger Stafford 2016년 4월 9일
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(:)]
  댓글 수: 1
Roger Stafford
Roger Stafford 2016년 4월 9일
@Azzi: I think these will not be in the same order requested by Zhongruo.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by