Loop for nested matrix multiplication
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello guys,
My problem is the following.
I have two matrices: a 155*3 matrix and a 465*3 matrix. I have to multiply each 1x3 row (from the 155*3 matrix) with each consecutive 3x3 matrix from the 465*3 matrix.
Now the loop I tried did not work out and I just can't get my thought's around it.
Here's my code:
m = size(A,2);
n = B(D:E);
D = B(1,:);
E = B(1:3,:);
C = zeros(m,n);
for ii = 1:m
for jj = 1:n
C(ii,jj) = sum(A(ii,:).*B(:,jj).');
end
end
Where matrix A is the 155*3 matrix and matrix B is the 465*3 matrix.
Any suggestions how to adjust the loop?
Thanks a million.
Kevin
2 commentaires
the cyclist
le 27 Avr 2013
This code will not get past the line
n = B(D:E)
unless D and E were already define previously. Can you post the entire code?
What do you want the shape of the final output to be? 155*3?
Is this a school assignment?
Réponse acceptée
Andrei Bobrov
le 28 Avr 2013
eg:
A = randi(4,155,3);
B = randi(8,455,3);% your matrices A and B
one variant;
s = size(A);
C = zeros(s);
for j1 = 1:s(1)
C(j1,:) = A(j1,:)*B((j1-1)*s(2)+1:j1*s(2),:);
end
other variant
s = size(A);
C = zeros(s);
for j1 = 1:s(1)
C(j1,:) = B((j1-1)*s(2)+1:j1*s(2),:)*A(j1,:)';
end
0 commentaires
Plus de réponses (4)
Matt J
le 27 Avr 2013
Modifié(e) : Matt J
le 27 Avr 2013
Here's a way with very short loops, using my DOWNSAMPN utility below
a=reshape(A.',[],1);
C=bsxfun(@times,a,B);
C=downsampn(C,[3,1]);
function M=downsampn(M,bindims)
%DOWNSAMPN - simple tool for downsampling n-dimensional nonsparse arrays
%
% M=downsampn(M,bindims)
%
%in:
%
% M: an array
% bindims: a vector of integer binning dimensions
%
%out:
%
% M: the downsized array
nn=length(bindims);
[sz{1:nn}]=size(M); %M is the original array
sz=[sz{:}];
newdims=sz./bindims;
args=num2cell([bindims;newdims]);
M=reshape(M,args{:});
for ii=1:nn
M=mean(M,2*ii-1);
end
M=reshape(M,newdims);
2 commentaires
Matt J
le 28 Avr 2013
Kevin van Berkel Commented:
Hello Matt,
Thank you for your response.
I tried your code but Matlab yields some errors. The first error is that the
function M=downsampn(M,bindims) is wrong. Matlab does not know what to do with the "function" part. The next error I get is:
Error using bsxfun Non-singleton dimensions of the two input arrays must match each other.
Error in mult (line 2) C=bsxfun(@times,a,B);.
Do you have any clue how to resolve this?
Cheers.
Matt J
le 28 Avr 2013
Modifié(e) : Matt J
le 28 Avr 2013
function M=downsampn(M,bindims) is wrong. Matlab does not know what to do with the "function" part.
You were supposed to put the code for downsampn in its own mfile. If that's not the problem, I need to see copy/pastes of the error messages.
Error using bsxfun Non-singleton dimensions of the two input arrays must match each other.
I don't receive this error. In all likelihood, it means that A is not 155x3 or B is not 465x3 like you posted. Here is the output of WHOS when I run. You should examine your matrix sizes in a similar way.
>> whos
Name Size Bytes Class Attributes
A 155x3 3720 double
B 465x3 11160 double
C 155x3 3720 double
a 465x1 3720 double
Matt J
le 29 Avr 2013
Modifié(e) : Matt J
le 29 Avr 2013
Another loopless way. Note that it would all be much simpler/faster (i.e., no transposes) if the data were organized in columns instead of rows
Ar=reshape(A.',1,3,[]);
Br=reshape(B.',3,3,[]);
Cr=sum(bsxfun(@times,Ar,Br),2);
C=squeeze(Cr);
Cr=mtimesx(Ar,Br,'t');
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!