フィルターのクリア

multiply and addition rows of matrices

2 ビュー (過去 30 日間)
Fiboehh
Fiboehh 2011 年 5 月 9 日
I got a matrix p = [ 1; 2 ; 3; 4] so in general dimension p(m) (=4) and a matrix w(m,n) (=4,2) witch is w= [ 2 4; 6 8; 1 3 ; 5 7] now i want to multiply each row of w(m,n) by the number of p(m). So multiplication row by row. So the result must be f=[ 2 4; 12 18; 3 9; 20 28]. i tried f=p*w but it gives not the right result.
Second all the rows of f must be add to eachother so f have only one row with elements the addition of all rows. In the example: f=[ 2+12+3+20 4+18+9+27] Thx in advance
  1 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 5 月 9 日
Well asked question! You provided a small example, what you've tried, what you expect and dimensions - everything we typically ask for.

サインインしてコメントする。

採用された回答

Sean de Wolski
Sean de Wolski 2011 年 5 月 9 日
Basic Singleton eXpansion along the 2nd dimension:
f = sum(bsxfun(@times,p,w))
  2 件のコメント
Fiboehh
Fiboehh 2011 年 5 月 9 日
Okj thx man, hadn't found that function :)
You know a solution for the addition, because f = bsxfun(@plus,p,p) doesnt change the dimension.
Sean de Wolski
Sean de Wolski 2011 年 5 月 9 日
You just use the 'sum' function as I have above. It defaults to summing columns.

サインインしてコメントする。

その他の回答 (1 件)

Bjorn Gustavsson
Bjorn Gustavsson 2011 年 5 月 9 日
Golf:
f = (w'*p)';
If you need the same function for complex numbers yuo might have to pay 2 extra characters:
f = (w.'*p).';

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by