Explicitly plot figures at the matrix index positions in grid
9 views (last 30 days)
Show older comments
I want to plot figures(ellipses) corresponding to the value of each element of matrix at their respective positions(Matrix Indices).
For example figure of index A(3,2) should get plotted at Index position 3,2.
5 Comments
Rik
on 10 Oct 2023
That isn't effective communication. What did you try? Why didn't it solve your issue? What should be different?
Do you want the ellipses in the same axes? Do you know how to plot one ellipse?
Answers (1)
Garmit Pant
on 9 Jan 2024
I understand that you are trying to plot ellipses in a figure with their position corresponding to their matrix indices.
Your desired plot can be achieved by using the “subplot” function. It will help you accommodate multiple plots in a single figure as a matrix. Please refer the following code snippet demonstrating how to use the function “subplot”.
% Example matrix A with some values
A = rand(3); % A 3x3 matrix with random values
% Define the figure
figure;
% Get the number of rows and columns
[rows, cols] = size(A);
% Loop through each element of the matrix
for i = 1:rows
for j = 1:cols
% Create a subplot for each matrix element
subplot(rows, cols, (i-1)*cols + j);
hold on; % Keep the subplot active to add the ellipse
% Get the value at the current index
value = A(i, j);
% Define the properties of the ellipse based on the matrix value
% Here we use a fixed size for demonstration, but you can scale it by 'value'
majorAxisLength = 0.4; % Major axis length
minorAxisLength = 0.2; % Minor axis length
% Calculate the angle for the ellipse points
theta = linspace(0, 2*pi, 50);
% Calculate the x and y coordinates for the ellipse points
x = majorAxisLength * cos(theta);
y = minorAxisLength * sin(theta);
% Plot the ellipse centered in the subplot
plot(x, y);
% Set axis limits and aspect ratio
axis([-0.5 0.5 -0.5 0.5]);
pbaspect([1 1 1]); % Set the aspect ratio to 1:1
% Remove axis ticks and labels
set(gca, 'xtick', []);
set(gca, 'ytick', []);
set(gca, 'xticklabel', []);
set(gca, 'yticklabel', []);
% Optionally, add a title or text to indicate the matrix value or index
title(sprintf('A(%d,%d)=%.2f', i, j, value));
hold off; % Release the subplot
end
end
% Adjust spacing between subplots if necessary
sgtitle('Ellipses for Each Matrix Element'); % Add a super title (requires R2018b or later)
For further understanding on the ”subplot” and “pbaspect” functions, you can refer to the following MATLAB Documentation:
- “subplot”- https://www.mathworks.com/help/matlab/ref/subplot.html
- “pbaspect” - https://www.mathworks.com/help/matlab/ref/pbaspect.html
I hope you find the above explanation and suggestions useful!
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!