1/X & X^-1 are they the same?
Show older comments
i'm about to write something that involves alot of reciprocals.
i want to know is 1/X the same as X^-1?
mathematically its the same, but which does matlab prefers? (that can make it simulate faster)
or are they really the same?
1 Comment
Walter Roberson
on 18 Jun 2012
Is X scalar or matrix?
If X is scalar, you should use the dot operators, ./ and .^
I do not know at the moment which would be faster or more accurate (if either.)
Accepted Answer
More Answers (3)
Rui Zhao
on 18 Jun 2012
2 votes
If X is a square matrix, 1/X shall be inv(X) since Matlab can't recognize 1/X for a matrix. Moreover, inv(X) is just the same for a square matrix as X^(-1).
For large matrix, the function inv() is well optimized by matlab and it costs less time than X^(-1). While for small matrix, their computational costs are comparable.
Titus Edelhofer
on 18 Jun 2012
0 votes
Hi Raymond,
if it's scalars take Walter's advice on ./ and .^ (should be no measurable difference between those). If X is a matrix, it depends, what you need the reziprocals for. For solving linear equations? In this case / (and \) are much preferable to ^(-1): / and \ solve linear systems, ^(-1) computes the inverse (which is a way to solve linear equations but a bad (unstable) one).
Titus
Jan
on 18 Jun 2012
The division is faster than the power operator. If you are in doubt, test it:
x = rand(1, 1e6);
tic;
for i = 1:length(x)
x(i) = 1 / x(i);
end
toc;
tic;
for i = 1:length(x)
x(i) = x(i) ^ -1;
end
toc;
Of course it would be faster to process the complete array at once in this example:
tic;
x = 1 ./ x;
toc;
tic;
x = x .^ -1;
toc;
Categories
Find more on Surrogate Optimization 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!