Why .^2 does not work as expected?
3 views (last 30 days)
Show older comments
Queila Martins
on 17 Jun 2016
Commented: Queila Martins
on 17 Jun 2016
Hi,
I'm doing some math calculations in a code. I noticed something strange in the code below:
a = imread('C:\tqPixels.png'); I = rgb2gray(a);
exp = I.^2
As tqPixels.png is an image with only 4 white pixels (all have 255 value), I thought that exp variable value would be each value powered to itself (255 * 255):
exp = [65025, 65025; 65025, 65025]
However, when I saw exp value in the results, it had the same values variable "I" had:
exp = [255, 255; 255, 255]
Sorry if this is very simple, but why .^2 does not work as it should?
I'm attaching the image and the code here for you to test. I really appreciate any suggestions on this.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/154062/image.png)
a = imread('C:\tqPixels.png');
I = rgb2gray(a);
exp1 = I.^2;
0 Comments
Accepted Answer
Roger Stafford
on 17 Jun 2016
I think you need to convert I to ‘double’ before doing the exponentiation on it. As it is, I believe it is ‘uint8’ type and anything above 255 is set down to just 255. Do this:
exp1 = double(I).^2
4 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!