Array indices must be positive integers or logical values.

xa=2
xb=3
ya=.9093
yb=.1411
x=0:1:6
for x=0:1:6
y(x)=((x-xb)/(xa-xb))*ya-((x-xa)/(xa-xb))*yb
end
plot(y,x)
why this code is showing "Array indices must be positive integers or logical values." this statement?

More Answers (2)

It's a FAQ so see the FAQ for complete info: Click here for the FAQ
In short, you can't start with x(0) since 0 is not an allowed array index. Try this:
xa=2
xb=3
ya=.9093
yb=.1411
x=0:1:6
for k = 1 : length(x)
thisX = x(k)
y(k)=((thisX-xb)/(xa-xb))*ya-((thisX-xa)/(xa-xb))*yb
end
plot(x, y, 'b*-', 'LineWIdth', 2, 'MarkerSize', 15)
grid on;
Hi,
Indeed, the indices in your case (x) have to be integers, like, 1,2,3,... can NOT be 0, -1, -2, 1.2, 2.5, etc. Index indicates the order (sequence) of the elements to be taken for processing/calculation/etc.
Here is an easy fix your problem:
xa=2;
xb=3;
ya=.9093;
yb=.1411;
x=0:1:6;
y = zeros(1, numel(x)); % Memory allocation to speed up the process that is helpful if your computation space is large.
for ii=1:numel(x)-1 % -1 needed since x starts at 0. The step = 1 (x=0:1:6) and thus, we can skip it (by default the step size is 1).
y(ii)=((x(ii)-xb)/(xa-xb))*ya-((x(ii)-xa)/(xa-xb))*yb;
end
plot(y,x), shg
Good luck

Categories

Tags

Community Treasure Hunt

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

Start Hunting!