Computing powers in matlab

find A whose ith element is A = ( 2.^i ) X ./ Y .... where X and Y are vectors of equal length
without looping. ie. X = [1 2 3] Y = [4 5 6].
I know how to do X ./ Y but the ith element of (2.^i) is confusing me and I do not know how to do it without looping. Thanks
Edit. Thank you 2.^(1:length(x)) worked. What if it was -2 instead of 2??? I tried fooling around with it but I had no luck

Antworten (3)

Mischa Kim
Mischa Kim am 16 Jan. 2014
Bearbeitet: Mischa Kim am 16 Jan. 2014

0 Stimmen

Does this do it?
X = [1 2 3]; Y = [4 5 6];
A = (2.^sort(randperm(length(X)))).*X./Y
A =
0.5000 1.6000 4.0000

1 Kommentar

Walter Roberson
Walter Roberson am 16 Jan. 2014
A little obfuscation to start the day right ;-)

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 16 Jan. 2014

0 Stimmen

2.^(1:3)

4 Kommentare

Mischa Kim
Mischa Kim am 16 Jan. 2014
Bearbeitet: Mischa Kim am 16 Jan. 2014
Am I reading the equation for A incorrectly? Does it not depend on X and Y? And yes, I did it for the general case of arbitrary vector sizes :)
the ith element of (2.^i) is confusing me
so I showed an example of creating that portion. I could have generalized that portion as
2.^(1:length(X))
which is what your code does in an obscure way.
Mischa Kim
Mischa Kim am 16 Jan. 2014
Ok, I understand. For the vector size portion, I have to admit, I tried to come up with a challenge for myself.
What if it was -2, mark asked.
(-2).^(1:length(X))

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 16 Jan. 2014

Kommentiert:

am 17 Jan. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by