Assigning values in a shape in an array

조회 수: 3 (최근 30일)
Shashvat Verma
Shashvat Verma 2019년 8월 11일
답변: Shashvat Verma 2019년 8월 11일
I have made an initial array, 161 x 161, of zeros:
x = (-80:80);
box = numel(x);
[X,Y] = meshgrid(x,x);
B = zeros([box, box]);
Then, I could make a circle in the array with value 10:
a = 10;
B(X.^2 + Y.^2 <= 100) = a;
However, I would like to make a semicircle of 'a' in the array, rather than the circle.
My code:
B(Y.^2<=abs(sqrt(100-X.^2))
However, this isn't giving me the correct shape.
What kind of things do I have to keep in mind to get the shapes I want?
Thanks.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 8월 11일
sqrt never returns a negative so the abs() only helps if 100-x^2 is negative in which case you are dealing with complex absolute value and the result comes out like sqrt(abs(100-x^2)). This is nothing like a semicircle.
You are also comparing the sqrt to y^2, so definitely not semicircle.
How do you want the semicircle to be oriented?
(Y>=0) & (X.^2 + Y.^2 <= 100)
For example.
Shashvat Verma
Shashvat Verma 2019년 8월 11일
Oh I see, this makes a lot more sense.
Thank you :)

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

채택된 답변

Shashvat Verma
Shashvat Verma 2019년 8월 11일
From Walter Robinson:
sqrt never returns a negative so the abs() only helps if 100-x^2 is negative in which case you are dealing with complex absolute value and the result comes out like sqrt(abs(100-x^2)). This is nothing like a semicircle.
You are also comparing the sqrt to y^2, so definitely not semicircle.
How do you want the semicircle to be oriented?
(Y>=0) & (X.^2 + Y.^2 <= 100)
For example.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by