for loop question

Hi all,

I'm a bit of a beginner with Matlab. I have an array P, and a single value P0. What I need to do is check P0 against every value in P. If P>=P0, y=1-(P0/P), otherwise y=0.

Here is a code I've written:
[code]
for ii=1
if P(ii)>P0
y=1-(P0/P(ii));
else
y=0;
end
ii=ii+1;
end
plot(P, y);
[/code]

However, all it does is plot a straight line along the x-axis. Can anyone see what might be wrong with my code.

Thanks

Comments

  • You have to index y.
    In a for-loop you can define initial and ending values.
    No need of ii = ii + 1

    for ix = 1 : length(P)
    if P(ix) > P0
    y(ix) = 1-(P0/P(ix));
    else
    y(ix) = 0;
    end
    end
    plot(y)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion