Shinobii
Shinobii

Reputation: 2571

MATLAB image summation confusion

I am trying to sum over my image (it is a 128x128 Uint8) in MATLAB (just a simple for loop), however, my sum will only go up to a value of 255. Afterwords it just keeps displaying 255 over and over again.

Does this mean that my variable has been assigned a Uint8 or something? If so how do I change this?

Cheers!

Upvotes: 1

Views: 534

Answers (1)

wakjah
wakjah

Reputation: 4551

Yes, presumably your data is of type Uint8. But you don't have to loop to sum, just use the sum function. Assuming your data is in x:

total = sum(double(x(:)))

sum will operate over a single dimension, so if you just passed it double(x) directly, it would return a 1x128 matrix; here we have passed it all the data reshaped to a single-dimension vector (using (:)), which has been converted to double using the double function.

Note that the type of your variable will be displayed along with its name and size in the Workspace window.

Upvotes: 4

Related Questions