how can i do a multiplication between a column and a row?

6 views (last 30 days)
when i make a product between a column and a row it says that it is incosistent row / column dimensions
  3 Comments
Voss
Voss on 3 Feb 2022
Those are both row vectors. A comma and a space do the same thing in concatenation, e.g., when defining a vector/matrix.
To make a column vector you can use semicolons:
A = [2 3 4];
B = [-2; 1; 3];
A*B
ans = 11
B*A
ans = 3×3
-4 -6 -8 2 3 4 6 9 12

Sign in to comment.

Accepted Answer

KSSV
KSSV on 3 Feb 2022
A = [2 3 4] ;
B = [-2 1 3] ;
C1 = A*B' % If you are expecting a dot product betwee them
C1 = 11
C2 = A.*B % element by element multiplication
C2 = 1×3
-4 3 12
C3 = A'*B % if you expecting a 3x3 matrix
C3 = 3×3
-4 2 6 -6 3 9 -8 4 12

More Answers (1)

Walter Roberson
Walter Roberson on 3 Feb 2022
The * operator is for algebraic matrix multiplication, also known as inner product. When you take A*B then size(A,2) must be the same as size(B,1) and the result is size(A,1) by size(B,2) . For example,
A=[2;3;4], B=[-2,1,3]
A = 3×1
2 3 4
B = 1×3
-2 1 3
size(A,2) == size(B,1)
ans = logical
1
C = A*B
C = 3×3
-4 2 6 -6 3 9 -8 4 12
[size(A,1), size(B,2)] == size(C)
ans = 1×2 logical array
1 1
So if you want to use the * operator between two row vectors of the same length, then you need to transpose one of the two. If you transpose the first of them then the result would be a square matrix; if you transpose the second of them then the result would be a scalar.
If you want to multiply A(1) by B(1) and A(2) by B(2) and so on, then that is not the * operator, it is the .* operator
A=[2,3,4], B=[-2,1,3]
A = 1×3
2 3 4
B = 1×3
-2 1 3
C = A .* B
C = 1×3
-4 3 12

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!