开发者

How to subtract a vector from each row of a matrix? [duplicate]

This question already has answers here: Closed 11 years ago.

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 s开发者_JAVA技巧econds.
Elapsed time is 0.713384 seconds.
Elapsed time is 2.621642 seconds.
Elapsed time is 1.323490 seconds.
Elapsed time is 17.269901 seconds.


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.


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); %...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜