Negative Numbers to the power of 0

6 vues (au cours des 30 derniers jours)
Simon Hunter le 18 Nov 2020
Commenté : TADA le 19 Nov 2020
Hi All,
New-again matlab user after a decade away; having trouble with raising a negative number to the power 0.
I recognise that:
• (-2)^0 = 1 and
• -2^0 = -1.
In my code, I am trying to populate a 5x5, row by row filling the columns with elements of one 1x5 row vector to the power of another 1x5 vector.
I have written a very basic, and probably inefficient way of doing this with two for loops, but when it comes to raising a negative number to the power 0 and storing it, I get 1 as opposed to what I think is correct; -1.
I presume Im missing something very basic but would really appreciate any help; Thank you!
k = [0:4]
x = [-2.0 3.0 9.0 2.0 25.0]
A = ones(5,5);
for row = 1:length(k)
for col = 1:length(k)
A(row,col) = x(col)^k(row);
end
end
This is what I get: But I think the top left should be -1 since -2^0 = -1
1 1 1 1 1
-2 3 9 2 25
4 9 81 4 625
-8 27 729 8 15625
16 81 6561 16 390625
15 commentairesAfficher 13 commentaires plus anciensMasquer 13 commentaires plus anciens
James Tursa le 18 Nov 2020
This is essentially the same example that Bruno gave. So the doc phrase "second from the right to left" means that you recursively do the operator that is second from the right as you process the operations. The rightmost operator is done last. E.g.,
>> 1.2^+1.3^+1.4^+1.5^+1.6^+1.7
ans =
1.6665
>> (1.2^+(1.3^+(1.4^+(1.5^+1.6))))^+1.7
ans =
1.6665
Good luck trying to remember that ... and better luck trying to get things straight if you mix in the regular ^ operator anywhere.
Bruno Luong le 18 Nov 2020
What a mess.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza le 18 Nov 2020
Modifié(e) : Ameer Hamza le 18 Nov 2020
You can do it without for-loop
k = 0:4;
x = [-2.0 3.0 9.0 2.0 25.0];
A = x.^(k.')
Also, x(col)^k(row) is equivalent to (-2)^0, not -2^0.
For example, what you you expect the output of following code to be
x = -2;
y = x^2
should y be 4 or -4?
1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Simon Hunter le 18 Nov 2020
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

TADA le 18 Nov 2020
Modifié(e) : TADA le 18 Nov 2020
Any arithmetic operation performed on variables will treat the variables as if it was wrapped with parentheses.
Try this simple example:
x = -2;
y = x^0
y = 1
In my opinion, this is the correct result, which takes into account the proper order of operations, but if you really want that to keep the original sign, you can multiply your result by the sign of the original number:
y = sign(x) * x^0
y = -1
3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Les Beckham le 19 Nov 2020
One caveat:
If x = 0, then the sign(x) * x^0 trick won't work to 'keep the original sign' while also returning the correct result.
Note that 0^1 = 1 but sign(0)*0^0 = 0, since sign(0) = 0 in Matlab.
TADA le 19 Nov 2020
Good point

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by