How to increase the speed of this code?
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
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)
댓글 수: 0
추가 답변 (1개)
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
2016년 4월 9일
@Azzi: I think these will not be in the same order requested by Zhongruo.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!