Loop for nested matrix multiplication
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
the cyclist
2013-4-27
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?
采纳的回答
Andrei Bobrov
2013-4-28
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 个评论
更多回答(4 个)
Matt J
2013-4-27
编辑:Matt J
2013-4-27
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 个评论
Matt J
2013-4-28
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
2013-4-28
编辑:Matt J
2013-4-28
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
2013-4-29
编辑:Matt J
2013-4-29
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!