How to form a matrix with the existing 7 matrices with following rules

1 Ansicht (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 26 Apr. 2020
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 26 Apr. 2020
Bearbeitet: Stephen23 am 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

Weitere Antworten (1)

John Hageter
John Hageter am 26 Apr. 2020
Try not assigning everything individually rather create a vector of input and reshape it
  2 Kommentare
Tianshu Gao
Tianshu Gao am 26 Apr. 2020
Thanks for your advice, I will try it.
Tianshu Gao
Tianshu Gao am 26 Apr. 2020
It works, even though it is a little bit complicated. Thank you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by