Improving MATLAB code ideas? - cells and matrices

6 次查看(过去 30 天)
Hello!
I have 2 sets of 2 matrices: a1,a2 and b1,b2. I calculate then c1 to c4 which are
c1=a1*b1
c2=a1*b2
c3=a2*b1
c4=a2*b2
I can also do this with cells and with for loops:
for j=1:2
for i=1:2
C{i,j}=A{i}*B{j};
end
end
where I have prealocated the cells and defined the cell A to contain all the elements in the set. Now the thing is, that the second way, because it uses for loops is twice as slow as the first method, but the first method (where I type every combination is not flexible because if the elements in a and b increase, I have to type it manually.
Do you know a third way, where I do not face this tradeoff between efficiency versus flexibility? Any ideas?
Much appreciated!
  14 个评论
Sean de Wolski
Sean de Wolski 2012-4-5
Boris, my code below _will_ do this for you. You just have to tell us how you're generating a1,a2...
If these are outputs from a function call or a data read, then this is simple. Stack the results into matrices similar to what I did below as you generate them (rather than generating a1, a2, ... an).
Boris
Boris 2012-4-10
Thanks for the effort Sean, I am checking it right now.

请先登录,再进行评论。

回答(2 个)

Jonathan Sullivan
You can use a very handy function created by James Tursa called mtimesx. It is for this exact type of problem. It does vectorized matrix multiplication.
You can setup your problem by first concatenating matrixes a1 and a2 into a variable A. We will concatenate them in the 3rd dimension.
A = cat(3,a1,a2);
We then concatenate matrixes b1 and b2 into variable B. We will do this in the 4th dimension. Since we want to do all combinations of A and B, it is important that A and B are concatenated in different dimensions.
B = cat(4,b1,b2);
Now for the calculation:
C = mtimesx(A,B);
C will be (in your case) a 2x2x2x2 array. It has arrays of matrixes. If you want to access a1*b1, you would call:
a1b1 = C(:,:,1,1);
Likewise, if you wanted to access a2*b1 you would call:
a2b1 = C(:,:,2,1);
James has written a rather well thought out help file at the link I attached above. You'll need to download it. Assuming you run windows, it should compile itself for you the first time you run it. It is very straight forward.
  1 个评论
Boris
Boris 2012-4-3
Thank you very much for the effort! I installed a compiler and tried it but unfortunately this is by far the slowest method for me. compared to the other 3 above in the code this one gave double the time from the standard "cell" code:
Elapsed time is 0.006381 seconds. - written out
Elapsed time is 0.012707 seconds. - cells
Elapsed time is 0.011784 seconds. - cells, matrices predefined
Elapsed time is 0.027363 seconds. - mtimesx

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2012-4-3
How are you generating a1,a2,b1,b2,etc.? Store the 'a's along the third dimension of a matrix and concatenate the 'b's columnwise. Example to follow. Then store the results in a numeric matrix rather than a cell. You know all of the 'submatrices' are square. So the results should be storable with each position known. Then run your for-loop accordingly along pages of the 'a' matrix using multiplying by the whole b at once.
clearvars;
sz = 3;
a = cat(3,magic(sz),ones(sz),pi*ones(sz)); %pagewise
b = a;
b = reshape(b,sz,numel(b)/sz); %columnwise
for ii = size(a,3):-1:1
iiv = ((ii-1)*sz+1):ii*sz;
M(iiv,:) = a(:,:,ii)*b;
end
Now the M matrix will contain all the results. This should be pretty optimized for speed right now. Let me know if anything isn't clear here.
More: You could actually skip the for-loop altogether by stacking the transposed submatrices, concatenating them column-wise and then transposing the result:
a = reshape(permute(a,[2 1 3]),sz,[])';
M2 = a*b;
Check
isequal(M2,M)
ans =
1
  3 个评论
Boris
Boris 2012-4-10
I tested the suggested routine and the results were unfortunately not much better than the other suggested answers. I went for the second suggestion - the matrix multiplication, avoiding the loop and I have to say, it looks pretty elegant. However I got 0.1363 secs just for the multiplication and afterwards I have to go 'into' the big matrix M2 and extract the resulting answers (c1 to c4), which takes a lot of time.
Elapsed time is 0.006541 seconds. - written out
Elapsed time is 0.012406 seconds. - cells
Elapsed time is 0.011982 seconds. - cells, matrices predefined
Elapsed time is 0.027814 seconds. - mtimesx
Elapsed time is 0.013632 seconds. - the matrix multiplication
With the extraction, (where the first thing that came to my mind is definetly not the fastest way to extract matrices from a big one) I jumped to 'Elapsed time is 0.066421 seconds.'
Boris
Boris 2012-4-10
clc
clear all
close all
B_LL0 = ones(4,4);
B_LL1 = ones(4,4);
F_0=randn(4,4);
F_1=randn(4,4);
B_TL00=zeros(4,4);
B_TL01=zeros(4,4);
B_TL10=zeros(4,4);
B_TL11=zeros(4,4);
n=1000;
tic
for i=1:n
F = cat(1,F_0,F_1);
B_LL = cat(2,B_LL0,B_LL1);
B_TL = F*B_LL;
% ns = size(F_0);
% B_TL00 = B_TL(1:ns,1:ns);
% B_TL01 = B_TL(ns+1:end,1:ns);
% B_TL10 = B_TL(1:ns,ns+1:end);
% B_TL11 = B_TL(ns+1:end,ns+1:end);
end
toc
tic
for i=1:n
B_TL00 = F_0*B_LL0;
B_TL01 = F_1*B_LL0;
B_TL10 = F_0*B_LL1;
B_TL11 = F_1*B_LL1;
end
toc
BT=cell(2,2);
BT{1,1}=zeros(4,4);
BT{2,1}=zeros(4,4);
BT{2,2}=zeros(4,4);
BT{1,2}=zeros(4,4);
F=cell(2,1);
BL=cell(2,1);
F{1}=F_0;
F{2}=F_1;
BL{1}=B_LL0;
BL{2}=B_LL1;
tic
for z=1:n
for j=1:2
for i=1:2
BT{i,j}=F{i}*BL{j};
end
end
end
toc
tic
for z=1:n
BT{1,1}=F{1}*BL{1};
BT{1,2}=F{1}*BL{2};
BT{2,1}=F{2}*BL{1};
BT{2,2}=F{2}*BL{2};
end
toc
tic
for z=1:n
A = cat(3,F_0,F_1);
B = cat(4,B_LL0,B_LL1);
C = mtimesx(A,B);
end
toc

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by