如何从矩阵的每一行中减去一个向量?
可能的重复:
如何划分每一行矩阵的固定行?
我正在寻找一种优雅的方法来从矩阵的每一行中减去相同的向量。这是一种不优雅的方法。
a = [1 2 3];
b = rand(7,3);
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
而且,优雅的方式不可能比这种方法慢。
我试过了
c = b-repmat(a,size(b,1),1);
,好像速度比较慢。
编辑:获胜者是这种方法。
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
编辑:更多方法和 tic toc 结果:
n = 1e6;
m = 3;
iter = 100;
a = rand(1,m);
b = rand(n,m);
tic
c = zeros(size(b));
for i = 1:iter
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
end
toc
tic
c = zeros(size(b));
for i = 1:iter
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
end
toc
tic
c = zeros(size(b));
for i = 1:iter
for j = 1:3
c(:,j) = b(:,j) - a(j);
end
end
toc
tic
for i = 1:iter
c = b-repmat(a,size(b,1),1);
end
toc
tic
for i = 1:iter
c = bsxfun(@minus,b,a);
end
toc
tic
c = zeros(size(b));
for i = 1:iter
for j = 1:size(b,1)
c(j,:) = b(j,:) - a;
end
end
toc
结果
Elapsed time is 0.622730 seconds.
Elapsed time is 0.627321 seconds.
Elapsed time is 0.713384 seconds.
Elapsed time is 2.621642 seconds.
Elapsed time is 1.323490 seconds.
Elapsed time is 17.269901 seconds.
Possible Duplicate:
How can I divide each row of a matrix by a fixed row?
I'm looking for an elegant way to subtract the same vector from each row of a matrix. Here is a non elegant way of doing it.
a = [1 2 3];
b = rand(7,3);
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
Also, the elegant way can't be slower than this method.
I've tried
c = b-repmat(a,size(b,1),1);
and it seems slower.
EDIT: The winner is this method.
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
EDIT: More methods, and tic toc results:
n = 1e6;
m = 3;
iter = 100;
a = rand(1,m);
b = rand(n,m);
tic
c = zeros(size(b));
for i = 1:iter
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
end
toc
tic
c = zeros(size(b));
for i = 1:iter
c(:,1) = b(:,1) - a(1);
c(:,2) = b(:,2) - a(2);
c(:,3) = b(:,3) - a(3);
end
toc
tic
c = zeros(size(b));
for i = 1:iter
for j = 1:3
c(:,j) = b(:,j) - a(j);
end
end
toc
tic
for i = 1:iter
c = b-repmat(a,size(b,1),1);
end
toc
tic
for i = 1:iter
c = bsxfun(@minus,b,a);
end
toc
tic
c = zeros(size(b));
for i = 1:iter
for j = 1:size(b,1)
c(j,:) = b(j,:) - a;
end
end
toc
results
Elapsed time is 0.622730 seconds.
Elapsed time is 0.627321 seconds.
Elapsed time is 0.713384 seconds.
Elapsed time is 2.621642 seconds.
Elapsed time is 1.323490 seconds.
Elapsed time is 17.269901 seconds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的贡献:
c = b - Ones(size(b))*diag(a)
现在对其进行速度测试:
结果:
经过的时间是 0.099979 秒。
不是速度相当快,但很干净。
Here is my contribution:
c = b - ones(size(b))*diag(a)
Now speed testing it:
The result:
Elapsed time is 0.099979 seconds.
Not quite as fast, but it is clean.
只有三个明显的答案,而你在问题中给出了其中两个。
第三种是按行,
但我预计它会比大型矩阵的按列处理慢,因为它访问的元素不符合内存顺序。
如果您将按列处理转变为 *.m 文件或子函数中的
for
循环,它是否仍然比repmat
版本更快?您可以测试速度的另一件事是:尝试预分配
c
。There are only three obvious answers, and you gave two of them in your question.
The third is by row,
but I'd expect that to be slower than your by-column processing for large matrixes since it accesses elements out of memory order.
If you turn your by-column processing into a
for
loop in a *.m file or subfunction, is it still faster than therepmat
version?One other thing you might test for speed: Try preallocating
c
.