Product of matrix with other matrix elements (or array cells)

%Solved by Oleg Komarov, see his reply below. And additionally by Oleg and Prabhakar for more advanced problem where A = [a b; c d] and a,b,c,d are rand(3,3).
%I'm not sure how to achieve the following:
%Matrix A
A = [2 0; 0 3]
%Matrix C
C = [1 -1;-1 1]
%How do I get each entry in matrix C multiplied by matrix A, to get a 4x4 matrix, as a function of A and C, without explicitly having to type:
[A -A;-A A]

 Accepted Answer

Elementwise multiplication:
A.*C
If A or C are cell arrays, then how are they stored? Whole matrix in a single cell or each element in single cells?
EDIT 1
Correction to misreading question:
A = [2 0; 0 3];
C = [1 -1;-1 1]
B = cell2mat(arrayfun(@(x) A*x,C,'un',0));
Or if you want B to be a 4 by 4 cell array, take the cell2mat off.
EDIT 2
I get smt different:
syms K1 K2
A = [K1 0
0 K2];
C = [1 -1 0
0 1 -1];
C.'*A*C
[ K1, -K1, 0]
[ -K1, K1 + K2, -K2]
[ 0, -K2, K2]
Oleg

7 Comments

They aren't stored as cell arrays but as matrices; as in the code given. I only mention arrays, because that might be the only thing to solve my problem. I guess I have to use arrays, because with just matrices your suggestion doesn't work yet.
Sry, misread your question. See edited solution above.
Awesome! Thanks a lot. I love it that MATLAB always seems to have a single line solution to any problem.
Here's the ackward multi-line solution I came up with in the meanwhile. I'll use your superior solution.
s=size(C,1);
for i=1:s
for j=1:s
n=(i-1)*s;
m=(j-1)*s;
B(n+1:n+s,m+1:m+s)=C(i,j)*A;
end
end
To apply to a more general context, your loop would be:
szC = size(C);
B = zeros(size(A)*2);
for n = 1:szC(2)
for m = 1:szC(1)
row = 2*n-1:n*2;
col = 2*m-1:m*2;
B(row,col) = C(n,m)*A;
end
end
Anyway, if you feel like my post answered your question, please accept the answer.
%Oleg, expanding my problem a bit, and hoping you can look at this; what should I do if I change the matrix A to itself be composed of different matrices?
%Matrix A
A = [K1 0; 0 K2]
K1=rand(3,3)
K2=rand(3,3)
%Matrix C
C = [1 -1 0;0 1 -1]
% how do I allow these different matrices K1 and K2, that are inside matrix A to become part of the calculation C'*A*C, so that
C'*A*C=[K1 -K1 0; -K2 K1+K2 -1; 0 -1 1]
Diederik, It is not possible to create A with K1 and K2 as matrices of size 3x3, you will get an error stating:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
I would recommend trying one of the following:
1) Create A with K1 and K2 as cell arrays, like so:
K2={rand(3,3)}
K1={rand(3,3)}
Then you can create A as : A = [K1, 0 ; 0 K2];
But even with this method of creation you will not be able to carry out the multiplication operation directly.
2) If you have access to the symbolic math toolbox, then it is possible to create A such that you get an answer to the multiplication that you wish to carry out. Consider the following example:
>> syms a b c d
>> A = [ a b ; c d]
>> a = rand(3,3)
>> b = rand(3,3)
>> c = rand(3,3)
>> d = rand(3,3)
>> C = [1 -1 0;0 1 -1]
>> Final = C'*A*C
Final =
[ a, b - a, -b]
[ c - a, a - b - c + d, b - d]
[ -c, c - d, d]
To access Final you can execute the command EVAL as follows:
>> eval(Final(1))
ans =
0.8143 0.3500 0.6160
0.2435 0.1966 0.4733
0.9293 0.2511 0.3517
and so on..
Oleg, sorry for the confusion; I wrote ones instead of K2's.
Oleg, Prabahkar, thanks for your answers, they're excellent!
I was indeed looking for the total solution eval(Final) for the same problem where b and c are zeros(3,3).
I can now take over the world. Thanks again!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!