Trying to Vectorise Cell Allocation to Remove For Loops
1 view (last 30 days)
Show older comments
I am trying to speed up my code and was wondering how I can vectorise my code to remove the for loops. The code essentially discretises space into cubes and this section finds the 8 coordinates which define each cube.
Thanks for all your help! :)
x=1:6
y=1:7
z=1:7
coord_matrix=cell(size(x,2)-1,size(y,2)-1,size(z,2)-1);
for ik=1:length(z)-1
for ij=1:length(y)-1
for ii=1:length(x)-1
coord_matrix(ii,ij,ik)={[x(ii),y(ij),z(ik);...
x(ii+1),y(ij),z(ik);...
x(ii),y(ij+1),z(ik);...
x(ii),y(ij),z(ik+1);...
x(ii+1),y(ij),z(ik+1);...
x(ii),y(ij+1),z(ik+1);...
x(ii+1),y(ij+1),z(ik);...
x(ii+1),y(ij+1),z(ik+1)]};
end
end
end
1 Comment
Walter Roberson
on 14 Oct 2023
Edited: Walter Roberson
on 14 Oct 2023
I did a vectorized 2D analog of this a couple of days ago; see https://www.mathworks.com/matlabcentral/answers/2032169-create-mesh-from-matrix#answer_1331984
You probably do not need to keep storing the coordinates over and over again: you can probably just store the indices. And besides, if you create the index array into a numeric array, you can use it for vectorized lookups of the coordinates if you really do need the array of coordinates.
Answers (2)
Ravi
on 14 Oct 2023
Hi Paolo Olson,
I understand you are trying to eliminate the nested for loops and want to optimize your code by switching to vectorization approach for finding the cube indices.
You can try using the “meshgrid” function,which is used to create a grid of indices.
- Create a grid of indices using meshgrid with ii, ij, and ik values where the ii values range from 1 to length(x) – 1, ij values range from 1 to length(y) – 1, and ik values range from 1 to length(z) – 1.
- Now you have the indices, and you need to apply the indexing on operation on arrays x, y, and z with the meshgrid indices. For this purpose, you can use the “reshape” function. For example, you can obtain the first coordinate using the following,
reshape(x(ii), [], 1), reshape(y(ij), [], 1), reshape(z(ik), [], 1)
For an in-depth understanding on “meshgrid” and “reshape” functions, kindly look at the below mentioned resources,
- meshgrid: https://in.mathworks.com/help/matlab/ref/meshgrid.html
- reshape: https://in.mathworks.com/help/matlab/ref/reshape.html
Hope the above-mentioned approach solves the issue you are facing.
0 Comments
Walter Roberson
on 14 Oct 2023
Your coordinates are the same size for each cuboid, and the size is predicatable. Use a numeric array instead of a cell array.
x=1:6
y=1:7
z=1:7
coord_matrix = zeros(size(x,2)-1,size(y,2)-1,size(z,2)-1, 8, 3);
for ik=1:length(z)-1
for ij=1:length(y)-1
for ii=1:length(x)-1
coord_matrix(ii,ij,ik,:,:)= [x(ii),y(ij),z(ik);...
x(ii+1),y(ij),z(ik);...
x(ii),y(ij+1),z(ik);...
x(ii),y(ij),z(ik+1);...
x(ii+1),y(ij),z(ik+1);...
x(ii),y(ij+1),z(ik+1);...
x(ii+1),y(ij+1),z(ik);...
x(ii+1),y(ij+1),z(ik+1)];
end
end
end
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!