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
      
      
 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). 
回答(2 个)
  Jonathan Sullivan
      
 2012-4-3
        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.
  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
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!