沿矩阵的每一行进行一维卷积 [英] Do 1-D convolution along each row of a matrix
问题描述
进行了快速搜索,找不到太多有关此的信息.假设我有一个2D矩阵和一个1D响应函数".我想用响应函数对2D矩阵的每一行进行卷积.我可以通过以下方式做到这一点:
Did a quick search and couldn't find much about this. Say I have a 2D matrix and a 1D 'response function'. I want to convolve each row of the 2D matrix with the response function. I can do this by:
for i=1:numrows
answer(:,i) = conv(2dmatrix(:,i),response_function,'same');
end
但是它太慢了!有什么技巧可以加速吗?
but it's super slow! Any tips to accelerate this?
谢谢
推荐答案
此代码在随机生成的矩阵上重现您的结果:
This code reproduces your results on randomly generated matrices:
conv2(response_function,1,2dmatrix,'same')
conv2
允许您分别沿行和列进行卷积,所以对行1
不执行任何操作,而对列response_function
进行卷积.
conv2
allows you to convolute along rows and columns separately, so do nothing to the rows, 1
, and convolve the columns by response_function
.
要沿每一行卷积,请交换前两个函数参数的顺序.
To convolve along each row, swap the order of the first two function arguments.
这篇关于沿矩阵的每一行进行一维卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!