Matrix Generation from Other Matrices

1 view (last 30 days)
Hello everyone, how can I write this matrix?
M = [C*B;
C*A*B + C*B;
C*A^2*B + C*A*B + C*B;
C*A^(n-1)*B + C*A^(n-2)*B ... C*B];
Where A, B, and C are matrices and n is an integer.
Size A = a,a Size B = a,b Size C = c,a

Accepted Answer

Stephen23
Stephen23 on 30 May 2020
Edited: Stephen23 on 30 May 2020
F = @(p)C*A^p*B;
M = arrayfun(F,0:n-1,'uni',0);
M = cumsum(cat(3,M{:}),3);
M = reshape(permute(M,[1,3,2]),[],b);
Tested:
>> n = 4;
>> a = 3;
>> b = 5;
>> c = 7;
>> A = randi(9,a,a);
>> B = randi(9,a,b);
>> C = randi(9,c,a);
>> F = @(p)C*A^p*B;
>> M = arrayfun(F,0:n-1,'uni',0);
>> M = cumsum(cat(3,M{:}),3);
>> M = reshape(permute(M,[1,3,2]),[],b)
M =
62 26 60 54 23
87 28 84 73 28
64 32 60 64 26
91 28 90 69 29
78 26 84 38 27
44 28 42 44 21
39 8 42 17 11
1097 364 1086 839 363
1234 413 1197 1024 406
1310 424 1308 962 430
1253 429 1215 1045 417
1173 428 1158 923 407
1119 364 1134 769 371
407 157 387 371 143
15024 5141 14643 12290 5011
16947 5684 16506 13853 5593
17592 6090 17118 14508 5898
17555 5869 17133 14231 5790
17456 5881 17151 13802 5799
15148 5313 14763 12446 5117
6315 2047 6219 4915 2060
209655 70440 204660 169953 69330
232333 78197 226401 189661 76832
247894 83060 242196 200194 81896
240128 80995 233967 196190 79492
240835 81632 234924 196069 79970
216015 72366 211302 173637 71400
84302 28771 81975 69536 28048
and compare against:
>> C*B
ans =
62 26 60 54 23
87 28 84 73 28
64 32 60 64 26
91 28 90 69 29
78 26 84 38 27
44 28 42 44 21
39 8 42 17 11
>> C*A*B + C*B
ans =
1097 364 1086 839 363
1234 413 1197 1024 406
1310 424 1308 962 430
1253 429 1215 1045 417
1173 428 1158 923 407
1119 364 1134 769 371
407 157 387 371 143
>> C*A^2*B + C*A*B + C*B
ans =
15024 5141 14643 12290 5011
16947 5684 16506 13853 5593
17592 6090 17118 14508 5898
17555 5869 17133 14231 5790
17456 5881 17151 13802 5799
15148 5313 14763 12446 5117
6315 2047 6219 4915 2060
  3 Comments
Stephen23
Stephen23 on 30 May 2020
Edited: Stephen23 on 30 May 2020
"Actually my matrices are 2D, the code didn't work when I defined my matrices as:"
A = randi(a,a);
B = randi(a,b);
C = randi(c,a);
Of course that didn't work, those commands will produce nonsense matrix sizes that are nothing like the sizes that you specified in your question! In contrast, the commands I used give exactly the matrix sizes that you specified in your question (did you actually check their sizes?):
B = randi(a,b); % What you did (wrong size) -> bxb matrix max value a
B = randi(9,a,b); % What I did (correct size) -> axb matrix max value 9
The randi documentation explains very clearly that its first input specifies the maximum value returned by randi (its first input does not specify anything about the output size as you have incorrectly assumed). Most likely you mixed up the randi syntax with the rand syntax, but in fact they have different calling syntaxes.
I suggest that you:
  • read the randi documentation to know what its inputs actually do.
  • take a peek at the sizes of the matrices that you are creating (hint: Variable Viewer).
Saleh Msaddi
Saleh Msaddi on 30 May 2020
My bad. I was confused between rand and randi, and I thought that the code produces an array (9,a,a). Anyway thanks Stephen.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!