How to speed up for-loop for creating cell with coordinate numbers as contents

2 次查看(过去 30 天)
I have the following script that constructs a 3D cell where the contents of each cell element are its XYZ coordinates. There has to be a faster way to do this, but I can't think of it. Anyone have advice? Here's the current (very slow!) for-loop:
MatchDimensions = [512 512 414];
X = MatchDimensions(1);
Y = MatchDimensions(2);
Z = MatchDimensions(3);
MatchCoordinates = cell(X, Y, Z);
for z = 1:Z
for y = 1:Y
for x = 1:X
MatchCoordinates{x, y, z} = [x y z];
end
end
end

采纳的回答

Matt J
Matt J 2014-8-14
编辑:Matt J 2014-8-14
Cells are not a great way to store large data and should be unnecessary when the data are all the same size. But, you could try this:
[X,Y,Z]=ndgrid(1:512,1:512,1:414);
MatchCoordinates=reshape(num2cell([X(:),Y(:),Z(:)],2),[512,512,414]);
  2 个评论
Matt J
Matt J 2014-8-14
It would be better to organize your data like this,
>> MatchCoordinates=reshape([X(:),Y(:),Z(:)].',3,512,512,414);
>> MatchCoordinates(:,2,4,5)
ans =
2
4
5

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by