".*" does not work need help

2 ビュー (過去 30 日間)
Yildirim Kocoglu
Yildirim Kocoglu 2018 年 6 月 23 日
編集済み: Jan 2018 年 6 月 23 日
https://www.mathworks.com/help/matlab/ref/times.html
The documentation above is for element-wise multiplication.
When I try to code one of the examples in there, it gives me an error : "Error using .* Matrix dimensions must agree."
a = [1 2 3]
a = 1×3
1 2 3
b = (1:6)'
b = 6×1
1
2
3
4
5
6
a.*b
ans = 6×3
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
6 12 18
It also does not work with what I tried at the beginning and also gives me the same error (reason why I looked for the documentation):
theta = 1X2 matrix
X = 5X2 matrix
answer = theta.*X
I also tried:
times(theta,X)
which does not work as well
I need someone to confirm if it works on their version of the matlab and if it does work what could possibly be the problem on mine?
My version of the matlab is the academic version but, I believe it should work just like the commercial one and I believe ".*" worked for me before ...
Thank you
  1 件のコメント
Stephen23
Stephen23 2018 年 6 月 23 日
編集済み: Stephen23 2018 年 6 月 23 日
@Yildirim Kocoglu: what release of MATLAB are you using? (this does not mean "academic", but something like "R2012b", or "R2017a"... that is what we need to know).
"reason why I looked for the documentation"
In order to know what your installed MATLAB does you should always use the installed documentation. The online documentation is interesting to read, but it bears no relationship to what you have installed.

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

回答 (1 件)

Jan
Jan 2018 年 6 月 23 日
編集済み: Jan 2018 年 6 月 23 日
Since Matlab R2016b the elementwise multiplication uses "arithmetic expanding": The singleton dimensions are expanded to match the other operand. See e.g. Loren: arithmetic expanding
In older Matlab versions this was done by bsxfun :
a = [1 2 3]
b = (1:6)'
bsxfun(@times, a, b)
a .* b % Working since R2016b

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by