\$\begingroup\$
\$\endgroup\$
Ax, Ay, Az: [N-by-N]
B=AA
(dyadic product)
means
B(i,j)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)]
B(I,j)
: a 3x3 matrix.
One way to construct B
is:
N=2;
Ax=rand(N); Ay=rand(N); Az=rand(N);
t=1;
F=zeros(3,3,N^2);
for i=1:N
for j=1:N
F(:,:,t)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)];
t=t+1; %# t is just a counter
end
end
%# then we can write
B = mat2cell(F,3,3,ones(N^2,1));
B = reshape(B,N,N)';
B = cell2mat(B);
Is there a faster way than this, especially when N
is large?
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Jun 5, 2011 at 4:22
1 Answer 1
\$\begingroup\$
\$\endgroup\$
There is a problem with vector multiplication in the second loop. You should transpose the second vector before doing multiplication.
F(:,:,t)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)]';
One of the ways to speed things up is to apply vectorization instead of two for
loops, such as with Dyadics.
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Dec 21, 2011 at 10:10
Explore related questions
See similar questions with these tags.
lang-matlab