Assigning values in a shape in an array

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Walter Roberson
Walter Roberson il 11 Ago 2019
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 il 11 Ago 2019
Oh I see, this makes a lot more sense.
Thank you :)

Accedi per commentare.

Risposta accettata

Shashvat Verma
Shashvat Verma il 11 Ago 2019
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.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by