![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/708812/image.jpeg)
Draw circle and ellipse in matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all, I am using the following code to draw a circle in a given matrix. However, it only works for a squared matrix, if I change the size of the matrix to something else (for example 90 by 100), it won't work. Please help me out. I am new to MATLAB. Thank you.
nx=100;
ny=100;
domain=ones(nx,ny);
[cols rows]=meshgrid(1:nx,1:ny);
centerX = 40;
centerY = 40
radius = 15
circle = (rows - centerY).^2 + (cols - centerX).^2 <= radius.^2
domain(circle)=2;
imagesc(squeeze(domain(:,:)));
Thanks a lot,
0 Kommentare
Antworten (2)
Simon Chan
am 11 Aug. 2021
Modify the third line:
nx=100;
ny=90;
domain=ones(ny,nx);
[cols rows]=meshgrid(1:nx,1:ny);
centerX = 40;
centerY = 40
radius = 15
circle = (rows - centerY).^2 + (cols - centerX).^2 <= radius.^2
domain(circle)=2;
imagesc(domain); % Or keep your original code
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/708812/image.jpeg)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!