How to increase the speed of this code?

1 回表示 (過去 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 Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by