0
votes

Im trying to multiply a Matrix by its first column. I have tried this code:

A = imread('cameraman.tif');
x0 = A(:,1);
y = A*x0;

But I get the following error:

Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.

and when I change my code to: y = A.*x0; again I get:

Error using .* Matrix dimensions must agree.

When I use whos command, I get this: A is a 256x256 Matrix and x0 is a 256x1 matrix. I dont know whats wrong with my code.

3

3 Answers

2
votes

imread is returning integer values. You must first convert them to floating point numbers with double prior to performing the multiplication:

A = imread('cameraman.tif');

% Explicitly convert from integer datatype to double
A = double(A);

% NOW perform your multiplication
y = A * A(:,1);

The important thing to look at with whos is the datatype which is listed in the "Class" column:

A = imread('cameraman.tif');t
whos('A')

%      Name           Size               Bytes  Class     Attributes
%    
%      A            256x256              65536  uint8 
1
votes

As stated earlier you need to convert the data to double precision. You can do this by using the function im2double. This function will normalize the data as well.

a = uint8(randi([0,100],3,3))
b=im2double(a)
b*255
b(1,:)*b
0
votes

You will need to cast the matrix to double or single.

Ad = double(A);

or:

As = single(A);

Since this is an image matrix, note that imshow expects values to be in the range of [0,255] for uint and [0,1] for single/double. So either cast back to integers or scale before displaying.