I'm trying to plot a graph with if than statements but keep getting the error "Array indices must be positive integers or logical values."
29 次查看(过去 30 天)
显示 更早的评论
function x= HW2Problem1(y)
x=1:.001:10;
n=length(x);
for i=(0:n)
if x(i)==0
y(i)=16.67;
elseif 0<x(i) & x(i)<5
y(i)=2*x(i);
elseif x(i)==5
y(i)=-91.67;
else
y(i)=10;
end
end
plot(x,y)
0 个评论
采纳的回答
Star Strider
2025-2-26
The definition of ‘positive integer’ are integers greater than zero.
So use:
for i=(1:n)
instead of:
for i=(0:n)
There are still problems, however your funciton no longer throws that error. (It also needs an additional end that I provided.)
y = sort(randn(1,25))
x = HW2Problem1(y)
plot(x,y)
function x= HW2Problem1(y)
x=1:.001:10;
n=length(x);
for i=(1:n)
if x(i)==0
y(i)=16.67;
elseif 0<x(i) & x(i)<5
y(i)=2*x(i);
elseif x(i)==5
y(i)=-91.67;
else
y(i)=10;
end
end
end
I leave the rest to you.
.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!