Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: creation of 3D array, vectorizing vs. looping

I have searched for an answer for my question on here but cannot find one, so I apologize in advance if it already exists!

What I am trying to do is create a 3D array of 3-d points in space (x,y,z). I know in a 1D vector you can specify the interval, like 1:5:20, to get a vector from 1 to 20 spaced by 5. What I would like to do is create a 3D array, most likely row by row would be the most efficient, where the spacing is by a unit vector (ix, iy, iz). so, for example,

    a(1,1,:) = [1, 1, 1]
    uv = [0.5 0.5 0.5]
    a(2,2,:) = [1.5, 1.5, 1.5]

etc. I know the numbers are not 'unit vectors', but the idea is there. Is there something along the lines of a = [1, 1, 1] : uv : [end, end, end] ???

like image 259
JoeMcG Avatar asked May 24 '26 17:05

JoeMcG


2 Answers

You might be interested in a mesh grid.

like image 71
BenH Avatar answered May 30 '26 13:05

BenH


An example:

[X,Y,Z] = meshgrid(1:0.1:2, 1:0.1:2, 1:0.1:2);    %# they can be different
points = [X(:) Y(:) Z(:)];

plot3(points(:,1),points(:,2),points(:,3),'.')
box on, axis equal
xlabel x, ylabel y, zlabel z

screenshot

like image 24
Amro Avatar answered May 30 '26 13:05

Amro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!