Assembling two matrices into one in a given pattern

Hello everbody,
I am fairly new to Matlab and I have some difficulties solving a task from university.
The task ist to assemble two matrices into one global matrices in such form:
A = [ 1 2; 1 2 ]
B = [2 3; 2 3]
% Assemble A and B into C
C = [1 2 0; 1 4 3; 0 2 3]
I tried it using a for loop but I can't manage to insert them into the given form in C.
When searching for a solution I came across "indexing" but I couldn't figure out how to use it in my problem.
I am grateful for any and all help!

3 件のコメント

Jan
Jan 2021 年 1 月 20 日
There is an infirnite number of algorithms, which produces C basend on A and B. Without seeing the exact instructions, it is unlikely that the readers guess, what you want to implement.
Dario Garic
Dario Garic 2021 年 1 月 20 日
編集済み: Dario Garic 2021 年 1 月 20 日
Hello Jan, thanks for your time!
I can assemble the matrix any way I want, as long as it works.
Yes you are right, I didn't explain the problem efficient enough, sorry!
I have n matrices in the form 2 x 2. I need to assemble those (Matrix A and B) into a global matrix C with the form n+1 x n+1.
For example:
I have n = 2. That means I have to allocate two 2 x 2 matrices into a global matrix 3 x 3.
I know that there is the function blkdiag, but there is a catch to the task. The number from A (2,2) needs to be added with the number in B(1,1) in the global matrix C.
The code should work for any number n.
Does this descripton help?
Jan
Jan 2021 年 1 月 20 日
See my edited answer.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2021 年 1 月 20 日
編集済み: Jan 2021 年 1 月 20 日

0 投票

A bold guess (although a wrong guess might be more confusing than posting nothing):
A = [1 2; 1 2];
B = [2 3; 2 3];
[s1, s2] = size(A);
C = zeros([s1, s2] + 1);
C(1:s1, 1:s2) = A;
C(2:s1+1, 2:s2+1) = C(2:s1+1, 2:s2+1) + B;
[EDITED after your comment]
List = {[1 2; 1 2], [2 3; 2 3], [4 7; 8 9]};
n = numel(List);
C = zeros(n + 1, n + 1);
for k = 1:n
C(k:k+1, k:k+1) = C(k:k+1, k:k+1) + List{k};
end

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 1 月 20 日

コメント済み:

Jan
2021 年 1 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by