3-dimensional matrix

4 Ansichten (letzte 30 Tage)
Kylie
Kylie am 7 Mär. 2012
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?

Antworten (2)

Sean de Wolski
Sean de Wolski am 7 Mär. 2012
some possibilities:
doc ndgrid
doc reshape
doc permute

Walter Roberson
Walter Roberson am 7 Mär. 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 Kommentare
Kylie
Kylie am 7 Mär. 2012
All of the distances from each grid point to the random points are calculated using a loop:
distances = zeros(1,n); % allocate storage vector for distances
for ii = 1:length(xg); % create index values for x
for jj = 1:length(yg); % create index values for y
for kk = 1:n; % create index values for observables
distances(kk) = norm([xg(ii),yg(jj)]-xy(kk,:),2);
Then they were put into a matrix called "Dist" to find the maximum distance at each grid point..
Dist(ii,jj)= max(distances);
Walter Roberson
Walter Roberson am 7 Mär. 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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by