# 3d Matrix - Extract Subarray and Multiply by Conjugate Transpose without Forloops

2 Ansichten (letzte 30 Tage)
Science Machine am 6 Sep. 2022
Bearbeitet: Science Machine am 8 Sep. 2022
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 Kommentare15 ältere Kommentare anzeigen15 ältere Kommentare ausblenden
Matt J am 8 Sep. 2022
Probably, but you haven't explained what t and r are.
Science Machine am 8 Sep. 2022
Bearbeitet: Science Machine am 8 Sep. 2022
t and ras parameters are the ii and jj indices from above u of size 1:100 and 1:10000, and the vector \$\$ has parameters jj also.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Matt J am 7 Sep. 2022
Bearbeitet: Matt J am 7 Sep. 2022
[m,n,p]=size(A);
Ar=reshape(A,[],p).';
UU = reshape(Ar,p,1,[]).*conj(reshape(Ar,1,p,[]));
UU=reshape(UU,p,p,m,n); %obtain the (i,j)-th outer product as UU(:,:,i,j)
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Matt J am 7 Sep. 2022
Bearbeitet: Matt J am 7 Sep. 2022
A typo. I fixed it.
Science Machine am 7 Sep. 2022
How did you learn to do such things? I knew reshape function but it did not occur to me to use it here.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by