Multiplying matrices as cells - 4 dimensional with variable no. of matrices (code works now!)

조회 수: 6(최근 30일)
Tom 2013년 4월 5일
%I'm trying to work with 4 dimensional cells containing matrices that need multiplying together.
%I have three matrices, A_1, A_2 & B, which are multiplied together to get Y. Each matrix - A_1, A_2 & B has different values depending on frequency, f.
%I've done this the long way first - with three separate frequency vectors: -
%% 1ST METHOD
f1 = 20
f2 = 500
f3 = 10000
%Then building the matrices, A_1, A_2 & B: -
A_1_f1 = [(f1 + 0) (f1 + 1);(f1 + 2) (f1 + 3)]
A_1_f2 = [(f2 + 0) (f2 + 1);(f2 + 2) (f2 + 3)]
A_1_f3 = [(f3 + 0) (f3 + 1);(f3 + 2) (f3 + 3)]
A_2_f1 = [(f1 + 1) (f1 + 2);(f1 + 3) (f1 + 4)]
A_2_f2 = [(f2 + 1) (f2 + 2);(f2 + 3) (f2 + 4)]
A_2_f3 = [(f3 + 1) (f3 + 2);(f3 + 3) (f3 + 4)]
B1 = [(f1 + 6);(f1 + 7)]
B2 = [(f2 + 6);(f2 + 7)]
B3 = [(f3 + 6);(f3 + 7)]
%And simple multiplication: -
Y1 = A_1_f1*A_2_f1*B1
Y2 = A_1_f2*A_2_f2*B2
Y3 = A_1_f3*A_2_f3*B3
%RESULTS: -
%Y1 = [48966;53738],
%Y2 = [509543046;511579178],
%Y3 = [4.0038e+12;4.0046e+12]
%THAT'S THE 1ST LONG WAY!
%% 2ND METHOD
%Then I've successfully reduced this so that f1, f2 & f3 become the vector, f: -
f = [20 500 10000]
A_1(1,1,:) = f + 0
A_1(1,2,:) = f + 1
A_1(2,1,:) = f + 2
A_1(2,2,:) = f + 3
A_2(1,1,:) = f + 1
A_2(1,2,:) = f + 2
A_2(2,1,:) = f + 3
A_2(2,2,:) = f + 4
B(1,1,:) = f + 6
B(2,1,:) = f + 7
ref(1,1,:) = f
ref(2,1,:) = f %(this is just a reference matrix for the next part)
Y = arrayfun(@(ind)A_1(:,:,ind)*A_2(:,:,ind)*B(:,:,ind)...
,1:size(ref,3),'uniformOutput',false);
Y = cat(3, Y{:});
%RESULS: -
%Y =
% val(:,:,1) =
% 48966
% 53738
% val(:,:,2) =
% 509543046
% 511579178
% val(:,:,3) =
% 4.0038e+12
% 4.0046e+12
%THAT'S THE MEDIUM - STILL POTENTIALLY LONG WAY - DEPENDING ON HOW MANY 'A' MATRICES THERE ARE.
%% 3RD METHOD
%This is good, but now I need to introduce more 'A' matrices, like A_3, A_4, A_5 etc.
%I've tried using 4-D cells, so that A_1, A_2, A_3, A_4... become just A, like this: -
A(1,1,1,:) = f + 0
A(1,2,1,:) = f + 1
A(2,1,1,:) = f + 2
A(2,2,1,:) = f + 3
A(1,1,2,:) = f + 1
A(1,2,2,:) = f + 2
A(2,1,2,:) = f + 3
A(2,2,2,:) = f + 4
%but that's where I get stuck. I'm thought a for loop method would work, but I couldn't get it work. I basically don't know how to multiply these out so I get the same results as above.
%If anyone knows how to do this that would be great - many thanks in advance.

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric Wannaz 2013년 4월 5일
편집: Cedric Wannaz 2013년 4월 5일
As you have only a limited number of values for f, you should avoid using complicated 3D or 4D arrays (indexing them is not always faster than looping indexing based on simpler 2D arrays), or solutions based on ARRAYFUN, and go for a much simpler solution based on a cell array for storing Y's.
Also, your matrices are quite simple so you could define them in a simpler way that you are doing above, i.e. you seem to have (for any given value of f):
A1 = f + [0 1; 2 3]
A2 = A1 + 1
B = f + [6; 7]
Using a cell array for storing results Y, you could implement the following:
f = [20 500 10000] ;
n = numel(f) ;
Y = cell(n,1) ;
for k = 1 : n
A1 = f(k) + [0 1; 2 3] ;
Y{k} = A1 * (A1 + 1) * (f(k) + [6; 7]) ;
end
Solutions could be then accessed as follows:
Y{1}
ans =
48966
53738
Y[2}
...
etc
Note that this solutions is flexible, as the size of the cell array and the upper bound of the loop adapt to the number of elements in the f vector. Also, I assumed that you just need Y's in the end and not successive values of A1, A2, and B. You could use a few extra cell arrays if you needed to store them.
댓글 수: 10표시숨기기 이전 댓글 수: 9
Cedric Wannaz 2013년 4월 9일
You're welcome! The profiler is a great tool; if you look at the number of calls of what takes time in your code, you'll see that no operation is inherently slow, but that you are repeating them millions times because you address/index arrays element per element. If you can vectorize these operations and reduce 10e6 loops 1 or 500 whether you fully vectorize or vectorize by row/column, you win.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!