# Multiplication of Array of Matrices with Array of scalars

14 views (last 30 days)
Rasa Smith on 10 Mar 2018
Edited: Rasa Smith on 12 Mar 2018
Hello, can anyone help me ? I did some operations with scalar matrix: Example:
A= [1 2 3 4 5];
C= [0.5 0.25 0 0 0.25; 0.25 0.5 0.25 0 0; 0 0.25 0.5 0.25 0; 0 0 0.25 0.5 0.25; 0.25 0 0 0.25 0.5];
A*C=[ 2.25 2 3 4 3.75]; % usual in matrix multiplication
Now my task is to change A elements into 2x2 matrices and then to do the same operation, for example, C is the same matrix:
AA= [ [1.75 4;-0.25 -0.25] [1.55 4;-0.25 -0.45] [1.75 4;-0.25 -0.25 ] [1.65 4;-0.25 -0.35] [1.15 4;-0.25 -0.85]];
% I construct this as a cell array AA is cell 1x5, where each variable is a 2x2 matrix
I want to multiply each variable in a cell array (which is a 2x2 matrix) coefficients in C, if to make clear, the multiplication should be performed like this;
AA*C = [ [1.75 4;-0.25 -0.25]*0.5+ [1.55 4;-0.25 -0.45] *0.25+0+0+[1.15 4;-0.25 -0.85]*0.25
[1.75 4;-0.25 -0.25]*0.25 +[1.55 4;-0.25 -0.45]*0.5+ [1.75 4;-0.25 -0.25 ]*0.25+0+0
0+[1.55 4;-0.25 -0.45] *0.25+[1.75 4;-0.25 -0.25 ]*0.5+[1.65 4;-0.25 -0.35]*0.25+0
0+0+[1.75 4;-0.25 -0.25 ]*0.25+[1.65 4;-0.25 -0.35]*0.5+[1.15 4;-0.25 -0.85]*0.25
[1.75 4;-0.25 -0.25] *0.25 +0+0+[1.65 4;-0.25 -0.35]*0.5+[1.15 4;-0.25 -0.85]*0.25 ];
How can I get this – to preserve usual matrix operation? Thank You.
##### 2 CommentsShowHide 1 older comment
Rasa Smith on 12 Mar 2018
Edited: Rasa Smith on 12 Mar 2018
Hello, I mean I have usual matrices A(1x5) and C(5x5) (in my real task it is not 5 but e.g. 10000). I multiply A*C as usual, the product A*C has format 1x5. In my real task I want to multiply AA (format 1x5 - which is a matrix of five matrices of format 2x2) by the same C (format 5x5) so that the product produces the result - 1x5 format matrix where each element is a 2x2 matrix. In other words, the task I want to perform has to give me a result - multiplication as matrix by scalar and then to sum, e.g. the first element of AA*C should be calculated:
[1.75 4;-0.25 -0.25]*0.5 +
[1.55 4;-0.25 -0.45]*0.25+
[1.75 4;-0.25 -0.25 ]*0+
[1.65 4;-0.25 -0.35]*0+
[1.15 4;-0.25 -0.85]*0.25
Then in my case the product of AA*C should be:
[ [1.55 4; -0.25 -0.45]
[1.65 4; -0.25 -0.35]
[1.675 4; -0.25 -0.325]
[1.55 4; -0.25 -0.45]
[1.425 4; -0.25 -0.575] ];
I hope I made it more easy to understand:) Thank you.

Ahmet Cecen on 10 Mar 2018
I believe you are over-complicating your problem by thinking as if you are working things out on a board. Your operation is separable and doesn't need you to store things in a cell array or represent them as matrices. From the exact assignment of AA in your question as an array:
AA = reshape(AA, 4,5);
Now each of your "matrices" is a column. Where:
AA*C(1,:)'
will give you first row of AA*C. If you have to return a matrix:
reshape(AA*C(1,:)',2,2);
Now you can loop over this to solve your problem. You can also store C as a cell instead and use cellfun, but shouldn't be necessary in your case.