Compute values in the nodes of a 3d matrix

조회 수: 1 (최근 30일)
gianluca
gianluca 2012년 9월 17일
Hi,
I've to compute values in the nodes of a 3d matrix. The coefficients vary along i and j index (X and Y direction). They vary also along the k index (Z direction) as function of the node position (above or below a surface).
% Create the surface:
x = rand(100,1)*4 - 2;
y = rand(100,1)*4 - 2;
S = x.*exp(-x.^2-y.^2) * 1000;
% Create positive coefficients
coeff = 0.02 + (0.08 - 0.02).*rand(100,1);
% Construct the interpolant:
F = TriScatteredInterp(x,y,S);
G = TriScatteredInterp(x,y,coeff)
% Evaluate the interpolant at the locations [XI,YI].
XI = -2:0.25:2;
YI = -2:0.1:2;
[XImat,YImat] = meshgrid(XI,YI);
ZImat = F(XImat,YImat);
C = G(XImat,YImat)
% Define a set of ZI locations
ZI = -500:100:500;
% Find the nodes below the surface S
BW = false(length(YI),length(XI),length(ZI));
for i = 1:length(YI)
for j = 1:length(XI)
BW(i,j,:) = ZImat(i,j) > ZI;
end
end
% Create a 3d grid where to compute the value in each node below the surface S
[X,Y,Z] = meshgrid(XI,YI,ZI);
index = BW >= 1;
T_geotherm = zeros(size(X));
% If I use a single coefficient (e.g. 0.003), this statement works
T_geotherm(index) = 18 + 0.003 .* Z(index);
But I would use the coefficients defined in C, with a command of the type:
for i = 1:length(XI)
for j = 1:length(YI)
T_geotherm(index) = 18 + C(i,j) .* Z(index);
end
end
Error message: Attempted to access C(1,18); index out of bounds because size(C)=[41,17].
Thanks for any suggestion, Gianluca

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 9월 18일
Please, try this is code:
% Create the surface:
x = rand(100,1)*4 - 2;
y = rand(100,1)*4 - 2;
S = x.*exp(-x.^2-y.^2) * 1000;
% Create positive coefficients
coeff = 0.02 + (0.08 - 0.02).*rand(100,1);
% Construct the interpolant:
F = TriScatteredInterp(x,y,S);
G = TriScatteredInterp(x,y,coeff);
% Evaluate the interpolant at the locations [XI,YI].
XI = -2:0.25:2;
YI = -2:0.1:2;
[XImat,YImat] = ndgrid(XI,YI);
ZImat = F(XImat,YImat);
C = G(XImat,YImat);
% Define a set of ZI locations
ZI = -500:100:500;
BW = bsxfun(@gt,ZImat,reshape(ZI,1,1,[]));
[Z,Z,Z] = ndgrid(XI,YI,ZI);
T_geotherm = 18 + bsxfun(@times,C,Z.*BW);
  댓글 수: 1
gianluca
gianluca 2012년 9월 18일
Hi Andrei, your code works, the values are computed in the T_geotherm matrix. I try to understand how the code works...
BW = bsxfun(@gt,ZImat,reshape(ZI,1,1,[]));
[Z,Z,Z] = ndgrid(XI,YI,ZI);
T_geotherm = 18 + bsxfun(@times,C,Z.*BW);
these three lines are unclear for me. If you had the time to give me some explanations, I would be very grateful.
Thanks very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 9월 17일
Change
[XImat,YImat] = meshgrid(XI,YI);
to be
[XImat,YImat] = ndgrid(XI,YI);
  댓글 수: 1
gianluca
gianluca 2012년 9월 18일
thanks, but it don't works. I've changed in
[XImat,YImat] = ndgrid(XI,YI);
With the command
for i = 1:length(XI)
for j = 1:length(YI)
T_geotherm(index) = 18 + C(i,j) .* Z(index);
end
end
I get the same results: in the T_geotherm matrix where I would compute the values there are NaN.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by