# vector multiplication .* vs *

164 ビュー (過去 30 日間)
Omar A Jimenez 2017 年 11 月 19 日

I have these two vectors,
u= [ 1 2 -1 2 1]
v=[ -1 0 2 0 1]
why does v*u give me an error, whilst u*v' give me a -2, which is the result of a scalar multiplication?
u and v have the same size, so shouldn't v*u go through?
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Stephen23 2017 年 11 月 19 日

@Omar A Jiminez: knowing the difference between matrix operations and element-wise operations is critical for being able to use MATLAB properly: not just multiplication, but transpose, division, and power too. You need to read the MATLAB documentation:

サインインしてコメントする。

### 採用された回答

Walter Roberson 2017 年 11 月 19 日
v*u is algebraic matrix multiplication, for which the rule is that for an (M x N) * (P x Q) operation, that N must equal P and the output size is M x Q -- so (M x N) * (N x Q) giving M x Q. You have (1 x 5) * (1 x 5) which violates that rule. When you take u * v' then you have (1 x 5) * (5 x 1) giving 1 x 1.
v.*u would go element by element, result(K) = v(K) * u(K) which would be 1 x 5 result.

サインインしてコメントする。

### その他の回答 (2 件)

Shomari Edwards 2019 年 8 月 29 日
24.5 + 64/3.52 + 8.3 · 12.53
3 76.428/15
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2019 年 8 月 29 日
It is not clear what you are trying to convey here?

サインインしてコメントする。

John Keevil 2022 年 2 月 18 日
The simple answer to the question is:-
u= [ 1 2 -1 2 1] and v=[ -1 0 2 0 1] are both row vectors. You cannot matrix multiply them because the number of rows and columns are not compatible for matrix multiplication. To multiply u*v by matrix multiplication requires the number of rows of u to equal the number of columns of v, which it does not since u has one row and v has 5 columns. To correct that, you have to transpose v, then it has one column. v' is the transpose of v. This why u*v' works.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by