".*" does not work need help

2 visualizzazioni (ultimi 30 giorni)
Yildirim Kocoglu
Yildirim Kocoglu il 23 Giu 2018
Modificato: Jan il 23 Giu 2018
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 Commento
Stephen23
Stephen23 il 23 Giu 2018
Modificato: Stephen23 il 23 Giu 2018
@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.

Accedi per commentare.

Risposte (1)

Jan
Jan il 23 Giu 2018
Modificato: Jan il 23 Giu 2018
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

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by