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

89 次查看(过去 30 天)
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.

采纳的回答

Matt J
Matt J 2024-12-30,3:35
编辑:Matt J 2024-12-30,3:52
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 个评论

请先登录,再进行评论。

更多回答(1 个)

Paul
Paul 2024-12-30,4:32
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

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by