How do I plot a 3-d matrix.( error with surf)
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a 321x60x120 matrix. The matrix has positive and negative float values. I would like to plot it. I tried surf. I get an error saying : "Error using matlab.graphics.chart.primitive.Surface
Value must be a scalar, vector or array of numeric type.". Values are all numeric. What could be wrong?
Is there any other plotting function, that would be recommended. The values are positions and momentums of a 2-d system, that evolve over time. time is the first dimension. The first 60 in the 3rd dimension are positions and the next 60 are momentum of this 2-d system. Ideally, what I want to do, is plot, for each iteration, a 3-d plot for each set of 60 rows and 60 columns of position and then a 3-d plot for each set of 60 rows and 60 positions of momentum, and show the evolution over time.
3 Commenti
Risposte (1)
Hitesh
il 29 Ago 2024
Hi Savitha,
The error you receive “Error using matlab.graphics.chart.primitive.Surface” suggests that the input that you passed as a parameter does not match specified dimension. You receive this error because you are passing a 3D matrix, whereas surf is allowed to only take 2D matrix as an input.
However, I understand that you are trying to visualize the evolution of position and momentum of a 2D system over time. For that you need to extract 2D slices from your 3D matrix. Since your matrix is 321x60x120, you can think of it as 321-time steps, with each time step containing a 60x120 matrix. This 60x120 matrix can be split into two 60x60 matrices: one for position and one for momentum.
Please refer to below code for reference:
data = rand(321, 60, 120);
for t = 1:size(data, 1)
% Extract the position and momentum matrices
positionMatrix = data(t, :, 1:60);
momentumMatrix = data(t, :, 61:120);
% Reshape to 60x60 matrices
positionMatrix = reshape(positionMatrix, [60, 60]);
momentumMatrix = reshape(momentumMatrix, [60, 60]);
% Plot positions
subplot(1, 2, 1);
surf(positionMatrix);
title(['Positions at time step ', num2str(t)]);
xlabel('X');
ylabel('Y');
zlabel('Position');
shading interp;
% Plot momentums
subplot(1, 2, 2);
surf(momentumMatrix);
title(['Momentums at time step ', num2str(t)]);
xlabel('X');
ylabel('Y');
zlabel('Momentum');
shading interp;
% Pause for animation effect
pause(0.1);
end
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!