Matlab中的逐元素矩阵乘法 [英] element by element matrix multiplication in Matlab
问题描述
所以我有以下矩阵:
A = [1 2 3; 4 5 6];
B = [0.5 2 3];
我正在MATLAB中编写一个函数,只要向量中的元素数与列数匹配,就可以使向量和矩阵乘以元素.在A
中有3列:
I'm writing a function in MATLAB that will allow me to multiply a vector and a matrix by element as long as the number of elements in the vector matches the number of columns. In A
there are 3 columns:
1 2 3
4 5 6
B
也有3个元素,因此应该可以使用.我正在尝试基于A
和B
产生以下输出:
B
also has 3 elements so this should work. I'm trying to produce the following output based on A
and B
:
0.5 4 9
2 10 18
我的代码如下.有人知道我在做什么错吗?
My code is below. Does anyone know what I'm doing wrong?
function C = lab11(mat, vec)
C = zeros(2,3);
[a, b] = size(mat);
[c, d] = size(vec);
for i = 1:a
for k = 1:b
for j = 1
C(i,k) = C(i,k) + A(i,j) * B(j,k);
end
end
end
end
推荐答案
引用MrAzzaman, bsxfun
是解决此问题的方法.但是,从您的函数名称来看,这看起来像是一项家庭作业,因此让我们继续使用您原来拥有的内容.这样,您只需要编写两个for
循环.您将使用第二个for
循环同时索引向量和矩阵的列.最外面的for
循环将访问矩阵的行.另外,您引用的是A
和B
,它们是代码中不存在的变量.您还将初始化输出矩阵C
始终为2 x 3 .您希望它的大小与mat
相同.我还删除了对向量长度的检查,因为您没有对结果进行任何操作.
Referencing MrAzzaman, bsxfun
is the way to go with this. However, judging from your function name, this looks like it's homework, and so let's stick with what you have originally. As such, you need to only write two for
loops. You would use the second for
loop to index into both the vector and the columns of the matrix at the same time. The outer most for
loop would access the rows of the matrix. In addition, you are referencing A
and B
, which are variables that don't exist in your code. You are also initializing the output matrix C
to be 2 x 3 always. You want this to be the same size as mat
. I also removed your checking of the length of the vector because you weren't doing anything with the result.
因此:
function C = lab11(mat, vec)
[a, b] = size(mat);
C = zeros(a,b);
for i = 1:a
for k = 1:b
C(i,k) = mat(i,k) * vec(k);
end
end
end
特别注意我的所作所为.最外面的for
循环访问mat
的行,而最里面的循环访问mat
的列以及vec
的元素.请记住,mat
的列数必须与vec
中的元素数相同.您应该在代码中检查一下.
Take special note at what I did. The outer-most for
loop accesses the rows of mat
, while the inner-most loop accesses the columns of mat
as well as the elements of vec
. Bear in mind that the number of columns of mat
need to be the same as the number of elements in vec
. You should probably check for this in your code.
如果您不喜欢使用bsxfun
方法,一种替代方法是取向量vec
并通过将向量vec
堆叠在此矩阵上来制作一个与mat
大小相同的矩阵. mat
中的行的次数是其自身次数的上限.此后,您可以进行逐元素乘法.您可以使用 repmat
重复此向量或您可以在任意维度上对矩阵进行给定的次数.因此,您的功能将简化为:
If you don't like using the bsxfun
approach, one alternative is to take the vector vec
and make a matrix out of this that is the same size as mat
by stacking the vector vec
on top of itself for as many times as we have rows in mat
. After this, you can do element-by-element multiplication. You can do this stacking by using repmat
which repeats a vector or matrices a given number of times in any dimension(s) you want. As such, your function would be simplified to:
function C = lab11(mat, vec)
rows = size(mat, 1);
vec_mat = repmat(vec, rows, 1);
C = mat .* vec_mat;
end
但是,我个人会使用bsxfun
路线. bsxfun
基本上是repmat
范例在后台执行的操作.在内部,它确保两个输入的大小均相同.如果不是,它将复制较小的数组/矩阵,直到与较大的数组/矩阵具有相同的大小,然后对两个变量中的对应元素进行逐元素运算. bsxfun
代表二进制单例扩展功能,这是一种准确地说出我刚才谈论的内容的方式.
However, I would personally go with the bsxfun
route. bsxfun
basically does what the repmat
paradigm does under the hood. Internally, it ensures that both of your inputs have the same size. If it doesn't, it replicates the smaller array / matrix until it is the same size as the larger array / matrix, then applies an element-by-element operation to the corresponding elements in both variables. bsxfun
stands for Binary Singleton EXpansion FUNction, which is a fancy way of saying exactly what I just talked about.
因此,您的功能进一步简化为:
Therefore, your function is further simplified to:
function C = lab11(mat, vec)
C = bsxfun(@times, mat, vec);
end
祝你好运!
Good luck!
这篇关于Matlab中的逐元素矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!