Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric types error when multiplying two 2 vectors in MATLAB

I have these 2 vectors:

alpha =
     1    1    1    1    1    1    1    1    1

f_uv =
   193  193  194  192  193  193  190  189  191

And when I do this:

alphaf_uv = alpha * f_uv'

I get the error message:

"??? Error using ==> mtimes
Integers can only be combined with integers of the same class, or scalar doubles." 

The interesting part is that this error doesn't appear if I define the same vectors in the console and try the multiplication there.

alpha is defined by me and f_uv is obtained from some pixels in a PNG image.

like image 806
Red33mer Avatar asked Nov 22 '08 10:11

Red33mer


People also ask

How do you multiply a vector in Matlab?

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

How do you multiply a number by a matrix in Matlab?

A = [1 1 0 0]; B = [1; 2; 3; 4]; Multiply A times B . The result is a 1-by-1 scalar, also called the dot product or inner product of the vectors A and B . Alternatively, you can calculate the dot product A ⋅ B with the syntax dot(A,B) .


2 Answers

Assuming they're both integer matrices to begin with, f_uv' may not be.

Try:

alphaf_uv = double(alpha) * double(f_uv')

and let us know if it still occurs.

You may need to turn alphaf_uv back into an integer type afterwards, depending on your needs.

like image 129
paxdiablo Avatar answered Sep 19 '22 12:09

paxdiablo


The big clue here is this:

alpha is defined by me and f_uv is obtained from some pixels in a png image.

This heavily implies that the f_uv data is coming in as uint8. The WHOS command will verify. When you define this at the command line, the vectors will be Double by default. That is why you are seeing the difference in behavior between "identical" matrices.

like image 36
MatlabDoug Avatar answered Sep 22 '22 12:09

MatlabDoug