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

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.
Many thanks for your time.

댓글 수: 2

I do not understand your notation. Is (9,9,25-n,2) a representation of array dimensions? If so then since n is 25, 25-n is 0 which would leave the empty array.
Apologies Mr Roberson. N is 5.

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

 채택된 답변

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

Many thanks for your detailed answer, I will study it and get back to you :)

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

추가 답변 (1개)

Alex
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

Thank you for your answer! I can do it for cells but can it be done using matrices? (no conversions between cells and matrices as the matrices already contain data)
I made a change that might suit you better, assuming I understand your matrix notation correctly.
Many thanks! Sorry I'm taking some time to examine your code step by step and implement it.

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

카테고리

도움말 센터File Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

질문:

2011년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by