필터 지우기
필터 지우기

How to plot a matrix in 3d?

조회 수: 25 (최근 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!