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

조회 수: 4 (최근 30일)
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
Stephen23
Stephen23 2020년 4월 26일
편집: Stephen23 2020년 4월 26일
"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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 4월 26일
편집: Stephen23 2020년 4월 26일
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

추가 답변 (1개)

John Hageter
John Hageter 2020년 4월 26일
Try not assigning everything individually rather create a vector of input and reshape it
  댓글 수: 2
Tianshu Gao
Tianshu Gao 2020년 4월 26일
Thanks for your advice, I will try it.
Tianshu Gao
Tianshu Gao 2020년 4월 26일
It works, even though it is a little bit complicated. Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by