Effacer les filtres
Effacer les filtres

Looping both rows and columns

3 vues (au cours des 30 derniers jours)
Peta
Peta le 19 Nov 2014
Commenté : Peta le 19 Nov 2014
I’m trying to do a simple script but I can’t seem to get it to do what I want! I’m very new to codewriting so I’m probably just missing something that’s obvious to other people, the code looks like this:
n=1;
i=1;
A=size(Production);
CasesToCompare=A(2);
while i<=CasesToCompare
while n<=length(Consumption)
if Consumption(n)>Production(n,i)
Cost(n,i)=(Consumption(n)-Production(n,i)).*BuyPrice(n);
else
Cost(n,i)= -((Production(n,i)-Consumption(n)).*SellPrice(n));
end
n=n+1;
end
i=i+1
end
I have data in my workspace called Consumption, Production, SellPrice, BuyPrice and what Im trying to do is first running the if/else statement through all the rows in the first column of Production and Consumption and saving it in the first column of a variable called Cost and then go to the next column of the Production variable (while still staying in the first column of the Consumption variable) and running through its rows and saving the results in the second column of “Cost”.
But the problem is that after the script Is done the output variable “Cost” only has one column. What am I doing wrong? Why does the number of columns in “Cost” not increase when “i” increases?
Thanks
  2 commentaires
Star Strider
Star Strider le 19 Nov 2014
I assume ‘Consumption’, ‘BuyPrice’, and ‘SellPrice’ are vectors, and ‘Production’ is a matrix.
How many rows does ‘Cost’ have? You don’t have a semicolon at the end of i=i+1 so you should see its output in the Command Window. Is it incrementing?
Peta
Peta le 19 Nov 2014
All the variables are hourly values during one year and when I imported them matlab calls them ”8760x1 double” (which I guess makes them vectors?) except for Production which contains the same number of rows but more columns, right now there is only two columns in it until I get this script to work and matlab calls it “8760x2 double” (which should mean it’s a matrix?).
And yes I can see both n and i incrementing which is why I don’t understand at all why the “Cost” variable isn’t growing.

Connectez-vous pour commenter.

Réponse acceptée

David Young
David Young le 19 Nov 2014
Try putting the line
n = 1;
between the two "while" statements. This variable needs to start at the beginning for each value of i.
It would be more conventional to do this with for loops, by the way, and in MATLAB you could make the code very concise by vectorising it. However, I'm sure it's best to get what you have working first.
  1 commentaire
Peta
Peta le 19 Nov 2014
Yees! I think I finally may have got it working by simply switching location of n=1; just like you said (at least "Cost" became a matrix)! Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by