フィルターのクリア

Write a function to calculate the area of a circle

219 ビュー (過去 30 日間)
Mr. NailGuy
Mr. NailGuy 2018 年 2 月 12 日
編集済み: DGM 2023 年 7 月 31 日
Hi everyone,
I need to write a function named areaCircle to calculate the area of a circle with the ff conditions: 1. The function should take one input that is the radius of the circle. 2. The function should work if the input is a scalar, vector, or matrix. 3. The function should return, one output, the same size as the input, that contains the area of a circle for each corresponding element. 4. If a negative radius passed as input, the function should return the value -1 to indicate the error.
I already have my code for the function that satisfies 1 to 3 except for 4. My code is as shown below:
function area = areaCircle(r)
if any(r<0)
A = r % array of random integers
negIndices = A < 0;
B = A; % copy A into new array B
B(negIndices) = -1 % replace the negative values in B with -1
area = B(negIndices);
else
area = pi*r.^2;
end
end
And the test inputs area:
r1 = 2;
area1 = areaCircle(r1)
r2 = [2 5; 0.5 1];
area2 = areaCircle(r2)
r3 = [1 1.5 3 -4];
area3 = areaCircle(r3)
area3 must give an array of values where the last element must be -1 to show an error. It must not be used for the calculation of the area. Thanks in advance!
  2 件のコメント
Jyothsna Chowdary
Jyothsna Chowdary 2022 年 6 月 14 日
hey can u tell me where to put the test inputs . i am not getting any output for it
Jose Raulito De Ocampo
Jose Raulito De Ocampo 2022 年 9 月 2 日
If you don't mind me asking, what does "negIndices" mean?

サインインしてコメントする。

採用された回答

Adam
Adam 2018 年 2 月 12 日
編集済み: Adam 2018 年 2 月 12 日
It isn't obvious from point 4 if it is expected that you just output -1 if any input is negative or -1 in only that location, but since you interpret it as the latter the simplest option would seem to be to use a logical mask as you have done, but you need to just always calculate
area = pi*r.^2;
Then you can just use the mask you created as B to overwrite the areas of any negative inputs with -1.
You can simplify the mask though as simply:
B = r < 0;
then
area( B ) = -1;
In your code you are not calculating any of the areas if any input is < 0. If that is what you want then it would seem that outputting just a scalar -1 would be sensible.
  5 件のコメント
SAMARTH SHAH
SAMARTH SHAH 2018 年 7 月 7 日
what exactly does area(B)=" some number" do? like B= 0 0 0 1 then what does -1 assigning do
Image Analyst
Image Analyst 2018 年 7 月 7 日
B is a binary image. If you use it as a logical index to an array, the operation will happen to those locations where B is true (1). So when B is a binary image of a circle, then setting area(B) to -1 will make the image of "area" have a value of -1 for every pixel in the circle defined by the "B" image.

サインインしてコメントする。

その他の回答 (3 件)

vedant mate
vedant mate 2018 年 12 月 5 日
You have just written a compicated code which is unecessary. You could simply do this:
function area = areaCircle(r)
area = pi*r.^2;
if any(r<0)
B = r < 0;
area (B) = -1;
end
%as simple as that:)
  3 件のコメント
ahmed samy
ahmed samy 2019 年 8 月 6 日
plz can you explain to me why you put area(B)=-1;
if i put
if any(r>0)
B=r>0;
area(B)=1;
then the output not the same ..... thanks for help
Stephen23
Stephen23 2023 年 7 月 9 日
@vedant mate: that IF you used is not required either.

サインインしてコメントする。


Garvit Amipara
Garvit Amipara 2019 年 5 月 14 日
編集済み: Garvit Amipara 2019 年 5 月 14 日
Here, with the following code you can give positive or negative- scalar or matrix input and get positive output.
function area = areacircle (r)
r = input('Enter the radius to calculate the area')
for j = size(r)
i = 1:j;
area(i) = 2 * pi * (r(i).^2) ;
if area(i) < 0;
B = -1 * area(i);
area = B
end
end
end
  1 件のコメント
DGM
DGM 2023 年 7 月 31 日
編集済み: DGM 2023 年 7 月 31 日
The input argument r is immediately discarded, and then the user is forced to manually enter an array for no good reason.
The loop indexing is unnecessary and will be wrong for anything other than a row vector.
Contrary to the requirements (though I think it's a better choice), the code appears like it intends to return negative areas instead of a simple -1 flag. Despite that difference, area(i) is never negative, so no negative inputs will ever be represented in the output in any manner. If area(i) were ever negative, the entire output would be set to a positive scalar, regardless of the size of the input -- so it's wrong either way.
To top it all off, the calculated areas are all incorrect by a factor of 2.
If it were intended to return negative areas instead of what the problem asks for, then
function area = areacircle(r)
area = pi*sign(r).*r.^2;
end

サインインしてコメントする。


Abdurasul Choriyorov
Abdurasul Choriyorov 2019 年 12 月 1 日
function area = areaCircle(r)
for c = 1:length(r)
if r(c)>=0
area = pi.*r.^2;
else
b = r<0;
area(b) = -1;
end
end
end
  1 件のコメント
DGM
DGM 2023 年 7 月 31 日
編集済み: DGM 2023 年 7 月 31 日
Assume all values are positive:
Consider a vector of length 100. You iterate through an unnecessary loop 100 times. Each time, you calculate all 100 outputs. The loop does nothing but waste time calculating the same thing and throwing it away 99 times.
Consider the following matrix: [1 2 3; 4 5 6]. You iterate through the loop 3 times, testing only the first three elements [1 4 2]. This is nonsense. Don't use length() unless you know what it does. As before, all intermediate results are simply discarded.
Now assume some values are negative:
The results will be incorrect unless r(max(size(r))) is negative.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by