3d Matrix - Extract Subarray and Multiply by Conjugate Transpose without Forloops
이전 댓글 표시
I have a 3d matrix A(i,j,k) of size [1:100,1:10000,1:989]. On the kth index I want to extract the 989 elements into a vector u and form the product u*ctranspose(u), for each of the
indices.
Using a double for loop, which is not something one should do in Matlab,
% this is an evil double for loop - I want to avoid doing this.
for ii=1:100
for jj=1:10000
u=squeeze(A(ii,jj,:));
% somehow compute and store uu in a vectorized way, but how?
uu=u*ctranspose(u); % note that uu is a 989x989 matrix, not a vector.
end %ii
end %jj
This would be really slow. Is there a vectorized way to the above, so I am not doing a double for loop?
댓글 수: 17
Yes, but the result will consume 7 TB in double floats
989^2*10000*100*8/2^40
Outer-products are a bad idea for many other reasons, too. What are you going to do with this even if you could store it?
Science Machine
2022년 9월 6일
Matt J
2022년 9월 6일
70 GB still seems like a lot. Do you have that much RAM?
Science Machine
2022년 9월 7일
Bruno Luong
2022년 9월 7일
What a waste of memory (and cpu) to explicitly expand rank-1 matrices.
Science Machine
2022년 9월 7일
Bruno Luong
2022년 9월 7일
Of course yours
Science Machine
2022년 9월 7일
@Science Machine The connection between xcorr and what you're attempting is not as apparent as you seem to think. A rank-1 matrix doesn't represent a correlation operation.
Science Machine
2022년 9월 7일
Matt J
2022년 9월 7일
It was not clear what @Bruno Luong meant - does he think it's okay to modify a single step of a procedure? And what is his alternative.
That depends on what you plan to do with uu. Note for example that if you have column vectors u and x, then this,
y=u*(u'*x);
produces the same result as this,
y=(u*u')*x;
but the former is much more efficient, both in terms of memory consumption and CPU time.
Science Machine
2022년 9월 7일
Science Machine
2022년 9월 8일
편집: Science Machine
2022년 9월 8일
Matt J
2022년 9월 8일
Probably, but you haven't explained what t and r are.
Science Machine
2022년 9월 8일
편집: Science Machine
2022년 9월 8일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Correlation and Convolution에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!