如何从矩阵的每一行中减去一个向量?

发布于 2024-10-22 12:32:17 字数 1551 浏览 2 评论 0原文

可能的重复:
如何划分每一行矩阵的固定行?

我正在寻找一种优雅的方法来从矩阵的每一行中减去相同的向量。这是一种不优雅的方法。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拧巴小姐 2024-10-29 12:32:17

这是我的贡献:

c = b - Ones(size(b))*diag(a)

现在对其进行速度测试:

tic
for i = 1:10000
    c = zeros(size(b));
    b = rand(7,3);
    c = b - ones(size(b))*diag(a);
end
toc

结果:

经过的时间是 0.099979 秒。

不是速度相当快,但很干净。

Here is my contribution:

c = b - ones(size(b))*diag(a)

Now speed testing it:

tic
for i = 1:10000
    c = zeros(size(b));
    b = rand(7,3);
    c = b - ones(size(b))*diag(a);
end
toc

The result:

Elapsed time is 0.099979 seconds.

Not quite as fast, but it is clean.

守望孤独 2024-10-29 12:32:17

只有三个明显的答案,而你在问题中给出了其中两个。

第三种是按行,

c(1,:) = b(1,:) - a; %...

但我预计它会比大型矩阵的按列处理慢,因为它访问的元素不符合内存顺序。

如果您将按列处理转变为 *.m 文件或子函数中的 for 循环,它是否仍然比 repmat 版本更快?

您可以测试速度的另一件事是:尝试预分配c

c = zeros(size(b));
c(:,1) = b(:,1) - a(1); %...

There are only three obvious answers, and you gave two of them in your question.

The third is by row,

c(1,:) = b(1,:) - a; %...

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 the repmat version?

One other thing you might test for speed: Try preallocating c.

c = zeros(size(b));
c(:,1) = b(:,1) - a(1); %...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文