필터 지우기
필터 지우기

I want to plot 2d quiver plot from 3d data but get a syntax error

조회 수: 5 (최근 30일)
clear;
nX = 7;
nY = 7;
nZ = 7;
gridSize = [nX nY nZ];
gridScaling = [1 1 1 1];
[X,Y,Z] = meshgrid(1:nX, 1:nY, 1:nZ);
X_labels = (X - worldCenter(2)) * gridScaling(2);
Y_labels = (Y - worldCenter(3)) * gridScaling(3);
Z_labels = (Z - worldCenter(4)) * gridScaling(4);
beta = cell(1,3);
for i = 1:3
beta{i} = zeros(nZ,nY,nX);
end
beta_x = zeros(gridSize);
beta_y = zeros(gridSize);
beta_z = zeros(gridSize);
for k = 1:gridSize(3)
for j = 1:gridSize(2)
for i = 1:gridSize(1)
beta{1}(k,j,i) = '<code determining the 1st component of the beta vector at coordinates i,j,k>';
beta{2}(k,j,i) = '<code determining the 2nd component of the beta vector at coordinates i,j,k>';
beta{3}(k,j,i) = '<code determining the 3rd component of the beta vector at coordinates i,j,k>';
end
end
end
quiver( X_labels, Y_labels, beta{1}( 4, 1:nY, 1:nX ), beta{2}( 4, 1:nY, 1:nX ), 0.7 );
The simplified code above doesnt work and I can't figure out why. I want to plot a slice of the vector field 'beta' at z == 4. I get "The size of Y must match the size of U or the number of rows of U.". But how can that be if it is set to nX by nY by definition?
Please help me to fix the problem.

채택된 답변

Cris LaPierre
Cris LaPierre 2024년 5월 3일
X_labels and Y_labels are 3D arrays of size 7x7x7 while U and V are 3D arrays of size 1x7x7.
Be sure to extract the corresponding slice of X and Y so they are the same size as U and V.
nX = 7;
nY = 7;
nZ = 7;
gridSize = [nX nY nZ];
gridScaling = [1 1 1 1];
[X,Y,Z] = meshgrid(1:nX, 1:nY, 1:nZ);
worldCenter = [0.1 0.5 0.3 1];
gridScaling = [2 3 2 4];
X_labels = (X - worldCenter(2)) * gridScaling(2);
Y_labels = (Y - worldCenter(3)) * gridScaling(3);
Z_labels = (Z - worldCenter(4)) * gridScaling(4);
beta = cell(1,3);
for i = 1:3
beta{i} = zeros(nZ,nY,nX);
end
beta_x = zeros(gridSize);
beta_y = zeros(gridSize);
beta_z = zeros(gridSize);
for k = 1:gridSize(3)
for j = 1:gridSize(2)
for i = 1:gridSize(1)
beta{1}(k,j,i) = X_labels(k,j,i).*exp(-X_labels(k,j,i).^2 - Y_labels(k,j,i).^2);
beta{2}(k,j,i) = Y_labels(k,j,i).*exp(-Y_labels(k,j,i).^2 - Z_labels(k,j,i).^2);
beta{3}(k,j,i) = Z_labels(k,j,i).*exp(-Z_labels(k,j,i).^2 - X_labels(k,j,i).^2);;
end
end
end
quiver( X_labels(4,:,:), Y_labels(4,:,:), beta{1}( 4, 1:nY, 1:nX ), beta{2}( 4, 1:nY, 1:nX ), 0.7 );

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by