Stuck on rewriting program to use nested if constructs

this is one of my homework problems that I'm currently stuck on. so far I have:

2 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 12 日
what's your problem now and upload your code instead of a photo
Rik
Rik 2018 年 11 月 12 日
Please post your code as code (see here for a GIF showing how to do it).
What are you stuck on? Doesn't this work? What error are you getting? If you don't know how to use fprintf have you read its documentation?
Also note that you made a small typo:
if x>=0
if y>=0 % you have y>0

回答 (2 件)

Torsten
Torsten 2018 年 11 月 12 日

0 投票

if x>=0
if y>=0
...;
else
...;
end
else
if y>=0
...;
else
...;
end
end
Guillaume
Guillaume 2018 年 11 月 12 日

0 投票

For reference, one way to do this in matlab without any if at all, in just one line:
fun = x.^(1 + (x<0)) + y.^(1 + (y<0))

2 件のコメント

Rik
Rik 2018 年 11 月 12 日
I was considering this approach as well. The middle ground between these would be to make it compatible with array inputs by using x(n) in a for-loop.
Guillaume
Guillaume 2018 年 11 月 12 日
The above is compatible with array inputs, as long as x and y are of compatible size.

この質問は閉じられています。

タグ

質問済み:

SP
2018 年 11 月 12 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by