Perform operation on matrix without for loop

So I have a 122x64x64 matrix x that has a grid of signals over time. First dim is time, second is x and third is y. Since the signals are all small signals with large DC offsets, I want to remove the offsets from the signals for all time values. The offsets for each signal are stored in a 64x64 matrix.
Here is the code I am currently using:
for a=1:64
for b=1:64
x(:,a,b) = x(:,a,b) - offset(a,b);
end
end
Is there a faster way to do this without a for loop? Later I want to do other similar operations, so a non-loop method would be preferred.

 採用された回答

Roger Stafford
Roger Stafford 2014 年 3 月 19 日

0 投票

[m,n] = size(offset);
x = bsxfun(@minus,x,reshape(offset,1,m,n));

その他の回答 (1 件)

José-Luis
José-Luis 2014 年 3 月 19 日
編集済み: José-Luis 2014 年 3 月 19 日

0 投票

EDIT
permute(bsxfun(@minus,permute(x,[2 3 1]),offset),[3 1 2]);

2 件のコメント

Shawn
Shawn 2014 年 3 月 19 日
This returns a mismatched dimensions error.
José-Luis
José-Luis 2014 年 3 月 19 日
編集済み: José-Luis 2014 年 3 月 19 日
My bad, forgot about non-singleton dimensions...
x = rand(128,64,64);
offset = rand(64);
tic
your_mat = permute(bsxfun(@minus,permute(x,[2 3 1]),offset),[3 1 2]);
toc
your_mat_1 = x;
for a=1:64
for b=1:64
your_mat_1(:,a,b) = x(:,a,b) - offset(a,b);
end
end
toc
all(your_mat(:) == your_mat_1(:))

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2014 年 3 月 19 日

編集済み:

2014 年 3 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by