This is a trivial question.
I have a matrix
B = [1 1 1; 5 5 5; 9 9 9; 2 2 2; 6 6 6; 10 10 10; 3 3 3; 7 7 7; 11 11 11; 4 4 4; 8 8 8; 12 12 12];
n = [1,1,1]
How can I find the dot product of each row of B with n?
I know I can use for loops and do this for all rows of B:
row1 = dot(B(i,:),n);
But there should be a better method using indexing......right? Using for loops isn't efficient.

 採用された回答

Titus Edelhofer
Titus Edelhofer 2015 年 8 月 14 日

1 投票

Hi,
multiplying each row with a vector is nothing else but the matrix vector multiplication:
B*n'
(n' since your n is a row vector).
Titus

3 件のコメント

Meghana Dinesh
Meghana Dinesh 2015 年 8 月 14 日
編集済み: Meghana Dinesh 2015 年 8 月 14 日
Right. works here.
What if I had another operation like minus?
I.e., if i want to subtract every row of B with n.
Resultrow_i = B(i,:)-n;
Or for any other algebraic operation for that matter...
Stephen23
Stephen23 2015 年 8 月 14 日
編集済み: Stephen23 2015 年 8 月 17 日
It depends on the operation, but many binary operations (e.g. basic arithmetic and logical comparison) are supported by bsxfun:
>> B = [1 1 1; 5 5 5; 9 9 9; 2 2 2; 6 6 6; 10 10 10; 3 3 3; 7 7 7; 11 11 11; 4 4 4; 8 8 8; 12 12 12];
>> n = [1,1,1];
>> bsxfun(@minus,B,n)
ans =
0 0 0
4 4 4
8 8 8
1 1 1
5 5 5
9 9 9
2 2 2
6 6 6
10 10 10
3 3 3
7 7 7
11 11 11
Meghana Dinesh
Meghana Dinesh 2015 年 8 月 14 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by