3-dimensional matrix
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a program that is in 2-dimensions and now I am trying to convert it to 3-dimensions. In the original program I have created a search grid with the syntax:
xg = xl:delta_x:xu;
yg = yl:delta_y:yu;
where
xl is the lowest value of the x plane
xu is the highest value of the x plane
delta_x is the gridline spacing in the x direction
yl is the lowest value of the y plane
yu is the highest value of the y plane
delta_y is the gridline spacing in the y direction
From these values I create a matrix [x,y] calculating distance from each grid point to 10 random points.
If I define a zl, zu, and delta_z...How do I create the 3-dimensional object/matrix holding these distances?
0 comentarios
Respuestas (2)
Sean de Wolski
el 7 de Mzo. de 2012
some possibilities:
doc ndgrid
doc reshape
doc permute
0 comentarios
Walter Roberson
el 7 de Mzo. de 2012
Is that "average" or "median" distance or some other kind of aggregate distance that you were calculating? If not then you would not have been able to store 10 distances at each (x,y), not unless you were using a cell array.
Anyhow, if you do have a single value per location, then
D = zeros(length(xg), length(yg), length(zg));
Depending on your calculations, you might not need to create this array in advance. For example,
[X,Y,Z] = ndgrid(xg, yg, zg);
D = sqrt((X - x0).^2 + (Y - y0).^2 + (Z - z0).^2);
Note here the use of ndgrid rather than meshgrid. meshgrid can only handle 2 dimensions. Be careful in changing from meshgrid to ndgrid as the first two outputs are not quite the same between the two.
2 comentarios
Walter Roberson
el 7 de Mzo. de 2012
[X,Y,Z] = ndgrid(xg, yg, zg);
for K = 1 : 10
D(:,:,:,K) = sqrt((X - xyz(K,1)).^2 + (Y - xyz(K,2)).^2 + (Z - xyz(K,3)).^2);
end
Dist = max(D,4);
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!