element-wise operator to scale rows of matrix with entries of a vector
12 ビュー (過去 30 日間)
古いコメントを表示
Given a matrix and a vector
A = [2 3 4 5;5 4 3 2;1 2 3 4;4 3 2 1];
d = [1 2 3 4];
I want to scale the rows of A with the corresponding entries of d to obtain
res = [2 3 4 5;10 8 6 4;3 6 9 12;16 12 8 4];
I figured out that
res=A.*d(:); --> correct
res2=A.*d; --> wrong, results in column-wise scaling
I know that .* indicates element-wise operations and d(:) re-shapes d into a column-vector, but my question is how to systematically read the expression A.*d(:) . Also, applies the element-wise operator .* to its left argument (here, A) or to its right argument (here, d)?
0 件のコメント
採用された回答
Matt J
2023 年 1 月 17 日
編集済み: Matt J
2023 年 1 月 17 日
Also, applies the element-wise operator .* to its left argument (here, A) or to its right argument (here, d)?
.* has two operands A and d, so it applies to both of them. It is also commutative (as you would expect), so you could have also done,
res=d(:).*A
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Exponents and Logarithms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!