"Matrix dimensions must agree" despite using .*

1 Ansicht (letzte 30 Tage)
Ivan Zackazow
Ivan Zackazow am 20 Apr. 2017
Beantwortet: Star Strider am 20 Apr. 2017
Matrix dimensions must agree.
Error in nnCostFunction (line 74) delta1_h = (d2).*(X1_tr);
d2 and X1_tr are the two vectors wich have sizes unsuitable for matrix multiplication, but my goal is element-wise multiplication and somehow Matlab fails to do it. Can you please help me with this problem?
In the console I've tried element-wise multiplication of [1 2 3] and [2; 3; 4; 5]. It was alright. Wonder what may be wrong here.
Thank you.

Antworten (1)

Star Strider
Star Strider am 20 Apr. 2017
It’s called ‘implicit expansion’ and is new in R2016b.
The ‘traditional’ (and my preferred) way to do this is to use the bsxfun function to achieve the same result:
a = [1 2 3];
b = [2; 3; 4; 5];
q1 = bsxfun(@times, a, b);
q2 = bsxfun(@times, b, a);
You will get the same result regardless of the way the operators are entered.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by