# Concatenation of matrices both vertically and horizontally in a specific order.

8 ビュー (過去 30 日間)
Tiffany 2011 年 11 月 21 日
I have two matrices.
n=5;
A = (9,9,n,2); %# 5 matrices to go along the diagonal
B = (9,9,25-n,2); %# 20 matrices going from left to right starting at row 1.
I would like to create a large matrix C with the following pattern. I've numbered matrices A and matrices in B as cells to demonstrate. There are 5 matrices in A, and 20 matrices in B as below.
A1 B1 B2 B3 B4
B5 A2 B6 B7 B8
B9 B10 A3 B11 B12
B13 B14 B15 A4 B15
B16 B17 B18 B19 A5
Matrix C is repeated twice as C(:,:,1) C(:,:,2)
Is it possible to automatically concatenate in this pattern so that I can increase the dimensions, say have up to A10 and still have the same matrix? (10 A matrices along the diagonal and the rest B matrices).
I'd like to avoid cell conversions as the matrices A and B already contain data.
##### 2 件のコメント表示非表示 1 件の古いコメント
Tiffany 2011 年 11 月 21 日
Apologies Mr Roberson. N is 5.

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

### 採用された回答

Andrei Bobrov 2011 年 11 月 21 日
A3d = randi(99,3,3,3)
B3d = randi([100,300],3,3,3^2-3)
n = eye(3)
nn = ~n
C(nn) = mat2cell(B3d,size(B3d,1),size(B3d,2),ones(size(B3d,3),1))
C = C'
C(~nn) = mat2cell(A3d,size(A3d,1),size(A3d,2),ones(size(A3d,3),1))
Cout = cell2mat(C)
variant
A3d = randi(99,3,3,3)
B3d = randi([100,300],3,3,3^2-3)
C3d = zeros(size(A3d,1),size(A3d,2),size(A3d,3)+size(B3d,3));
[n1,n2,n3] = size(C3d);
C3d(:,:,1:n1+1:end) = permute(A3d,[2 1 3]);
C3d(C3d==0) = permute(B3d,[2 1 3]);
b1 = bsxfun(@plus,(1:n1)',(0:n2-1)*n3);
b2 = bsxfun(@plus,(0:n1:n1*(n2-1))',(0:n1-1)*n3*n2);
Cout = zeros(n3);
Cout(bsxfun(@plus,b1,reshape(b2,1,1,[]))) = C3d;
Cout = Cout'
VARIANT 2
A = randi(99,4,2,4)
B = randi([100 300],4,2,4^2-4)
[q p n] = size(A);
N = n*p;
nC = n^2;
C = zeros(q,p,nC);
C(:,:,1:n+1:end) = A;
C(:,:,setdiff(1:nC,1:n+1:nC)) = B;
Cout = reshape(permute(reshape(permute(C,[2 1 3]),p,q,N/p,[]),[1 3 2 4]),N,[])'
##### 1 件のコメント表示非表示 なし
Tiffany 2011 年 11 月 22 日
Many thanks for your detailed answer, I will study it and get back to you :)

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

### その他の回答 (1 件)

Alex 2011 年 11 月 21 日
The following loop will auto fill in C for any size. Keep in mind, do to memory issues, it is always better to try and know the size of what you are trying to make first - that is, don't go back and try to make C bigger later.
Also, this way, you won't be able to reference A1(1,1,1,1) by using C(1,1,1,1,1,1,1); You would need to do:
tmp = C{1,1}; A_element = tmp(1,1,1,1);
n = #A matricies
C = cell(n);
for rows = 1: n
for col = 1:n
if( row == col )
C(row,col) = A(row);
else
C(row,col) = B( (col - row) + 5*(row - 1));
end
end
end
Edit:
Changed the algorithm to match what I understand to be the matrix size notiation
n = num rows
C = zeros(n, n, 9, 9, 2)
for rows = 1: n
for col = 1:n
if( row == col )
C(row,col,:,:,:) = A(:,:,row,:);
else
C(row,col,:,:,:) = B(:,:, (col - row) + 5*(row - 1),:);
end
end
end
##### 3 件のコメント表示非表示 2 件の古いコメント
Tiffany 2011 年 11 月 21 日
Many thanks! Sorry I'm taking some time to examine your code step by step and implement it.

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

### カテゴリ

Find more on Creating and Concatenating Matrices 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!