Use two bsxfun for row and column vector to span matrix without putting in memory

조회 수: 1 (최근 30일)
T
T 2017년 9월 29일
답변: Jan 2017년 9월 30일
Hello, I have a vector A of size 1xN and a vector B of size MxN I can span a MxN matrix temp with temp=bsxfun(@times,A,B);. Now the tricky part. I create this temp matrix to multiply the following to my data matrix:
data=data.*exp(1i.*temp);
Is there a way I can use bsxfun to directly apply this operation on data, without creating the large temp matrix in the memory?
Thanks
  댓글 수: 1
dpb
dpb 2017년 9월 29일
bsxfun created the temp in memory behind scenes anyway, so it didn't actually save memory in computation, just was transparent in not having the explicit temp variable.
Beginning w/ R2016b implicit expansion was introduced that will replace most uses of bsxfun with more efficient memory usage. If you have a release of that vintage or newer investigate that feature.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2017년 9월 30일
A = rand(1, 1e5);
B = rand(100, 1e5);
data = ones(size(B));
tic, for k = 1:5
temp = bsxfun(@times, A, B);
data = data .* exp(1i .* temp);
end, toc
tic, for k = 1:5
temp = bsxfun(@times, 1i * A, B);
data = data .* exp(temp);
end, toc
tic, for k = 1:5
data = data .* exp((1i * A) .* B); % Auto-expanding in >= R2016b
end, toc
Elapsed time is 2.421372 seconds.
Elapsed time is 2.857152 seconds.
Elapsed time is 2.136714 seconds.
Observation from the profiler:
data = data .* exp(1i .* temp);
takes the same time as
data = data .* exp(temp);
but
temp = bsxfun(@times, 1i * A, B);
or
temp = 1i * bsxfun(@times, A, B);
have 100% more runtime than
temp = bsxfun(@times, A, B);
If this piece of code is the bottleneck of your code, create C-Mex function.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by