How to plot a matrix in 3d?

37 ビュー (過去 30 日間)
Davide Cannavacciuolo
Davide Cannavacciuolo 2023 年 11 月 20 日
回答済み: Shubham 2023 年 12 月 3 日
I would like to plot a simple square matrix in a 3d plot, where in xlabel there is the index of rows or colums as well for the ylabel.
I would like to see the dots as values and not a surface.
Then can you also tell me how to view just one axis and the zaxis at the same time? the only option is to pan?

回答 (1 件)

Shubham
Shubham 2023 年 12 月 3 日
I understand that you want to plot a square matrix in 3D while using the row and column indices as x and y-axis and values as z-axis. You also want to view only one axes (x-axis or y-axis) and z-axis at the same time.
You can refer to the following code snippet below:
% Example square matrix of random numbers
A = randi(100,100);
[n, ~] = size(A);
% Create a grid of indices for the rows and columns
[X, Y] = meshgrid(1:n, 1:n);
% Flatten the matrices and plot
x = X(:);
y = Y(:);
z = A(:);
figure;
plot3(x, y, z, '.');
xlabel('Row Index');
ylabel('Column Index');
zlabel('Value');
grid on;
The above code plots the square matrix as desired. For viewing the plot on an x-z plane you can have look at the following code:
% Adjust the view to see only one axis and the z-axis
% For example, to see the x-axis (row index) and the z-axis
view(0, 0); % This sets the azimuth to 0 and the elevation to 0
% Alternatively, to see the y-axis (column index) and the z-axis
% view(90, 0);
% This sets the azimuth to 90 and the elevation to 0
I hope this helps!!

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!