How to store data from my IF loop

10 views (last 30 days)
IZ
IZ on 16 Mar 2017
Commented: IZ on 16 Mar 2017
Hello everyone, I need to create a vector based on some conditions I have on other variables. Basically, I have 672 entries (hour 1,2,3...) for q ,q1 and q2. q1 is priced at p1 and q2 is priced at p2. I basically want to write a new "column" or vector with the results from the loop: if less than q1, price is p1, if not, if up to q1+q2, the price is p2. However, I am not able to save this results for each one of the 672 hours. How could I do that?
if q=q1
p=p1
else if q=q2+q1
p=p2
else p=p3
end
end
Thank you!

Accepted Answer

Adam
Adam on 16 Mar 2017
Well, you don't have a loop at the moment. 'if' is not a loop, it is just a single statement.
Something similar to the following is what you need. I went off your words rather than code for the criteria in the if statements as your code does not match what you state as the conditions.
for n = 1:numel( q )
...
if q(n) < q1(n)
p(n) = p1(n);
elseif q(n) <= q2(n) + g1(n)
p(n) = p2(n);
else
p(n) = p3(n);
end
end

More Answers (1)

ES
ES on 16 Mar 2017
you wrap a for loop on top of your code.
for iloop=1:672
q=dataIn(iloop);
if q=q1
p=p1
else if q=q2+q1
p=p2
else p=p3
end
end
end
dataout(iloop) = p
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!