Why it is saying this? Index in position 2 is invalid. Array indices must be positive integers or logical values. Error in Homework (line 21) surf(x,y,F(i,j))

1 回表示 (過去 30 日間)
x=[-5:5]
y=[-5:5]
for i=1:(x-1);
for j=1:(y-1);
if (x(i)>=0)&(y(j)>=0)
F(i,j)=2.*x(i)+y(j)^2
end
if (x(i)>=0)&(y(j)<0)
F(i,j)=x(i)+y(j)
end
if (x(i)<0)& (y(j)>=0)
F(i,j)=x(i)^2+2*y(j)
end
if (x(i)<0)&(y(j)<0)
F(i,j)=x(i)^2+y(j)^2
else
end
end
end
figure
surf(x,y,F(i,j))

採用された回答

Alan Stevens
Alan Stevens 2022 年 2 月 3 日
Like this
x=[-5:5];
y=[-5:5];
for i=1:numel(x)
for j=1:numel(y)
if (x(i)>=0)&&(y(j)>=0)
F(i,j)=2.*x(i)+y(j)^2;
end
if (x(i)>=0)&&(y(j)<0)
F(i,j)=x(i)+y(j);
end
if (x(i)<0)&& (y(j)>=0)
F(i,j)=x(i)^2+2*y(j);
end
if (x(i)<0)&&(y(j)<0)
F(i,j)=x(i)^2+y(j)^2;
else
end
end
end
figure
surf(x,y,F)
  2 件のコメント
Akash Talapatra
Akash Talapatra 2022 年 2 月 3 日
why is this numel written within for loop? would you like to explain this a little bit?
Alan Stevens
Alan Stevens 2022 年 2 月 4 日
numel(x) means the number of elements in x. Your statements like
for i=1:(x-1);
don't make sense.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCode Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by