Concatenation of matrices both vertically and horizontally in a specific order.
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 21 Nov 2011
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.
Tiffany
il 21 Nov 2011
Risposta accettata
Più risposte (1)
Alex
il 21 Nov 2011
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
Categorie
Scopri di più su Numeric Types in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!