3d Matrix - Extract Subarray and Multiply by Conjugate Transpose without Forloops
Show older comments
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 Comments
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
on 6 Sep 2022
Matt J
on 6 Sep 2022
70 GB still seems like a lot. Do you have that much RAM?
Science Machine
on 7 Sep 2022
Bruno Luong
on 7 Sep 2022
What a waste of memory (and cpu) to explicitly expand rank-1 matrices.
Science Machine
on 7 Sep 2022
Bruno Luong
on 7 Sep 2022
Of course yours
Science Machine
on 7 Sep 2022
@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
on 7 Sep 2022
Matt J
on 7 Sep 2022
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
on 7 Sep 2022
Science Machine
on 8 Sep 2022
Edited: Science Machine
on 8 Sep 2022
Matt J
on 8 Sep 2022
Probably, but you haven't explained what t and r are.
Science Machine
on 8 Sep 2022
Edited: Science Machine
on 8 Sep 2022
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!