Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using simple matrix multiplication

I stumbled upon an error during a simple multiplication that rather surprised me. What is happening here, I always assumed * was only for matrix multiplication.

x = 2;
y = zeros(1,4);
y(1) = 1 *x;
y(2) = x* 1;
y(3) = (x *1);
y(4) = x *1;
y
x *1

Will give the following output:

y =

     2     2     2     1

Error: "x" was previously used as a variable,
conflicting with its use here as the name of a function or command.
See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use Command Syntax" for details.

Does anyone understand what is going on here? Of course I verified that x is not a function.

like image 777
Dennis Jaheruddin Avatar asked Aug 13 '13 10:08

Dennis Jaheruddin


People also ask

Can 2x3 matrix multiply with 2x2?

No, we cannot multiply a 2x3 and 2x2 matrix because for multiplying matrices, two matrices should be compatible. Since the number of columns in the first matrix(3) is not equal to the number of rows in the second matrix(2), we cannot perform matrix multiplication for this case.

Can you multiply a 2x3 matrix and a 4x3 matrix?

Matrix Multiplication is not Commutative Note that the multiplication is not defined the other way. You can not multiply a 3x4 and a 2x3 matrix together because the inner dimensions aren't the same. This product is undefined.

Can a 2x3 and 3x5 matrix be multiplied?

Multiplication of 2x3 and 3x5 matrices is possible and the result matrix is a 2x5 matrix. This calculator can instantly multiply two matrices and show a step-by-step solution.


1 Answers

It depends on the spacing. See also here for a longer explanation and some examples of when you could have genuine ambiguity, but basically the first three of these will work as you expected, and the last will assume you are trying to call a function x with input *1:

x*1  
x * 1 
x* 1
x *1

This doesn't happen if you assign the output to some variable, regardless of spacing:

y(2) = x *1
z = x *1
x = x *1
like image 118
nkjt Avatar answered Sep 23 '22 02:09

nkjt