Loop for nested matrix multiplication
Ältere Kommentare anzeigen
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 Kommentare
the cyclist
am 27 Apr. 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?
Kevin van Berkel
am 27 Apr. 2013
Akzeptierte Antwort
Weitere Antworten (4)
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 Kommentare
Matt J
am 28 Apr. 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.
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
Kevin van Berkel
am 28 Apr. 2013
Bearbeitet: Matt J
am 28 Apr. 2013
0 Stimmen
Kevin van Berkel
am 29 Apr. 2013
0 Stimmen
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');
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!