How can I loop with two variables, can't figure it out

1 visualizzazione (ultimi 30 giorni)
I am totally new to Matlab, and my master thesis require me to use matlab to do some analysis. I had a problem with loop. I wanna construct a loop with two variables for example
for i = 2:4
for j = 1:4
Ratio = Price(i,:)/Vol(j,:)
end
end
Price = 1 2 3 4
5 6 7 8
9 8 9 6
Vol = 2 3 4 5
6 7 8 9
1 2 3 4
and I want my answer to be
ratio = 2.5 2.0 1.8 1.6
1.5 1.1 1.1 0.7
The 2nd row of Price divide by the 1st row of Vol The 3rd row of Price divide by the 2st row of Vol and so on ...........until the end row of price then stop the loop.
Can it work if price and Vol has different number of row but they have some number of Column?
  2 Commenti
Stephen23
Stephen23 il 25 Gen 2015
Note that you should not use the variable names i and j, as these are the names of the inbuilt imaginary unit.
Matt J
Matt J il 25 Gen 2015
Modificato: Matt J il 25 Gen 2015
That's not a big issue anymore, now that the syntax 1i and 1j are supported,
>> for i=1:3, i+1i, end
ans =
1.0000 + 1.0000i
ans =
2.0000 + 1.0000i
ans =
3.0000 + 1.0000i

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 25 Gen 2015
Modificato: Matt J il 25 Gen 2015
Just do
Ratio=Price(2:4,:)./Vol(1:3,:)
  13 Commenti
Steven Kwok
Steven Kwok il 25 Gen 2015
I made it! Thank guys for your kind support.
Matt J
Matt J il 25 Gen 2015
Great. Could you hit this button, if you like the answer you got?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by