Why does this special case for associativity of a matrix product with a hadamard product hold true?

4 Ansichten (letzte 30 Tage)
I found this very useful mathematical property to hold true for a special case. Can someone show me why?
Let A = m x n matrix.
Let B = n x p matrix.
Let C = 1 x p vector.
When the hadamard product B .* C is performed, Matlab duplicates the rows of C n times to form a n x p matrix C1 with n identical rows.
Then the following expression holds true:
A x (B .* C1) = (A x B) .* C2 where Matlab again duplicates the rows of C, but this time the 1 x p row vector is duplicated m times to form m x p matrix C2.
Example:
A = rand(2,5);
B = rand(5,7);
C = rand(1,7);
D1 = A * (B .* C)
D1 = 2×7
1.6311 0.2174 1.4102 1.6453 0.1953 1.0507 0.6540 1.2156 0.2325 1.3234 1.1411 0.1932 0.9036 0.4533
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D2 = (A * B) .* C
D2 = 2×7
1.6311 0.2174 1.4102 1.6453 0.1953 1.0507 0.6540 1.2156 0.2325 1.3234 1.1411 0.1932 0.9036 0.4533
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D1 and D2 are identical.
The reason this is so useful for my application is that A and B are system matrices that are calculated only once, with m << n. C changes thousands of times, so the operation (A x B) .* C is many times faster than A x (B .* C). I need to show why this works mathematically so that I can write it up.
Thank you.

Akzeptierte Antwort

Matt J
Matt J am 30 Dez. 2024
Bearbeitet: Matt J am 30 Dez. 2024
It is clear why this happens when n=1, i.e, when A*B is an outer product. But now for arbitrary n, the matrix product can be written as a sum of outer products, so the result follows because Hadamard product distributes into addition.
  7 Kommentare
Matt J
Matt J am 30 Dez. 2024
Maybe this is what you are looking for:
where denotes a column vector of k ones.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Paul
Paul am 30 Dez. 2024
Hi Clay,
If you're willing to accept (or show, which I think would be straight forward) that, in Matlab, matrix .* row is equal to matrix*diag(row), then it follows that the two cases are equal to each other after converting both to ordinary matrix multiplication.
rng(100);
A = rand(2,5);
B = rand(5,7);
C = rand(1,7);
D1 = A * (B .* C);
D2 = (A * B) .* C;
norm(D1 - D2)
ans = 2.4991e-16
D1prime = A*(B*diag(C));
norm(D1-D1prime)
ans = 0
D2prime = (A*B)*diag(C);
norm(D2-D2prime)
ans = 0

Kategorien

Mehr zu Resizing and Reshaping 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