Draw circle and ellipse in matrix

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,

답변 (2개)

Simon Chan
Simon Chan 2021년 8월 11일

0 개 추천

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

댓글 수: 1

Anh Mai
Anh Mai 2021년 8월 11일
That's great, Simon. Is there any alternatives for the third line so that we can simply change the size of the domain without changing the code?. Thank you

댓글을 달려면 로그인하십시오.

Wan Ji
Wan Ji 2021년 8월 11일

0 개 추천

in line 3, do not use
domain=ones(nx,ny);
but use
domain=ones(ny,nx);

댓글 수: 1

Anh Mai
Anh Mai 2021년 8월 11일
Thanks Wan Ji, I am looking for an alternative that can replace the third line, so that we can only need to change the size (input of nx and ny) without changing the code.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 8월 11일

댓글:

2021년 8월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by