I want to plot 2d quiver plot from 3d data but get a syntax error
2 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!