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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by