How to multiply two rows or columns?

白昼怎懂夜的黑 提交于 2019-12-20 01:59:07

问题


a = [1, 2, 3];
b = [3, 2, 1];
c = a * b;

yields

error: operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3)

Why can I not multiply these two rows of the same size? I shouldn't have to run a for loop for this, but I don't know of another way...

I saw section 1.2.3 here, which indicates (to me at least) that I should be able to do it.


回答1:


You made 2 rows, which can't be multiplied together.

The general form of matrix multiplication is "Row-Dot-Column", which means take the dot product of each row with each column. In your case you have 1 row, but 3 columns (which doesn't work!).

a = [1, 2, 3];
b = [3, 2, 1];
c = a' * b;

ans =

 3     2     1
 6     4     2
 9     6     3




回答2:


I see now that there is a .* operator. I did not know where to find that in the documentation, and it does what I want.



来源:https://stackoverflow.com/questions/14764534/how-to-multiply-two-rows-or-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!