How to form a matrix with the existing 7 matrices with following rules
1 view (last 30 days)
Show older comments
Now, I have 7 matrices (with specific values) in total, W(6x6), C1(3x3), C2(3x3), C3(3x3),C4(3x3),C5(3x3),C6(3x3). I want to form a matrix A (18x18) with following rules:
A= [w(1,1)*C1 w(1,2)*C1 w(1,3)*C1 w(1,4)*C1 w(1,5)*C1 w(1,6)*C1
w(2,1)*C2 w(2,2)*C2 w(2,3)*C2 w(2,4)*C2 w(2,5)*C2 w(2,6)*C2
w(3,1)*C3 w(3,2)*C3 w(3,3)*C3 w(3,4)*C3 w(3,5)*C3 w(3,6)*C3
w(4,1)*C4 w(4,2)*C4 w(4,3)*C4 w(4,4)*C4 w(4,5)*C2 w(4,6)*C4
w(5,1)*C5 w(5,2)*C5 w(5,3)*C5 w(5,4)*C5 w(5,5)*C5 w(5,6)*C5
w(6,1)*C6 w(6,2)*C6 w(6,3)*C6 w(6,4)*C6 w(6,5)*C6 w(6,6)*C6]
Do I have a way to form matrix A without typing them manually? Because, in the future, I would probability solve 1000 C matrices, which is time consumming to type in manually. Thank you.
1 Comment
Stephen23
on 26 Apr 2020
Edited: Stephen23
on 26 Apr 2020
"Do I have a way to form matrix A without typing them manually?"
Of course, it is easy once you avoid using numbered variables.
"Because, in the future, I would probability solve 1000 C matrices, which is time consumming to type in manually."
Yes, that would be very time consuming. That is why no experienced MATLAB user defines lots of numbered variable names: because it wastes their time (code writing time, debugging time, runtime). Indexing is so much simpler and more efficient.
Accepted Answer
Stephen23
on 26 Apr 2020
Edited: Stephen23
on 26 Apr 2020
This is easiest when you avoid anti-pattern numbered variables and use one array, e.g. a cell array:
W = rand(6,6);
C = {rand(3,3),rand(3,3),rand(3,3),rand(3,3),rand(3,3),rand(3,3)}; % one array
N = numel(C);
A = kron(W,ones(3,3)).*repmat(vertcat(C{:}),1,N); % or REPELEM instead of KRON
Checking the first output submatrix (the others you can check yourself):
>> size(A)
ans =
18 18
>> A(1:3,1:3)
ans =
0.181146 0.014799 0.190315
0.100409 0.250922 0.047237
0.046128 0.018101 0.180526
>> W(1,1)*C{1}
ans =
0.181146 0.014799 0.190315
0.100409 0.250922 0.047237
0.046128 0.018101 0.180526
More Answers (1)
John Hageter
on 26 Apr 2020
Try not assigning everything individually rather create a vector of input and reshape it
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!