Пусть Matrices = [A B C D]
будет набором квадратных матриц. я хочу построить
H = [A B C D;
B A B C;
C B A B;
D C B A]
Если бы меня интересовал только корпус 4 на 4, то этого было бы достаточно. Однако я хочу построить аналогичную матрицу, когда Matrices = [A B C D E F]
и т. д. Какой код я могу написать для этого?
Это можно сделать следующим образом:
N = 2; % matrix size
M = 3; % number of matrices
matrices = arrayfun(@(x) {randi(9,N)}, 1:M); % input matrices
matrices_3D = cat(3, matrices{:}); % concatenate the matrices along the third dim
ind1 = repmat(reshape(1:N^2, N, N), M, M); % linear indices within each matrix
ind2 = (repelem(toeplitz(1:M), N, N)-1)*N^2; % linear indices to select the matrices
result = matrices_3D(ind1 + ind2); % build result
Пример запуска:
>> celldisp(matrices)
matrices{1} =
1 4
2 6
matrices{2} =
6 4
5 9
matrices{3} =
5 4
6 5
>> result
result =
1 4 6 4 5 4
2 6 5 9 6 5
6 4 1 4 6 4
5 9 2 6 5 9
5 4 6 4 1 4
6 5 5 9 2 6
Обозначение
Matrices = [A B C D]
сбивает с толку, так как предполагает массив Matlab. Пожалуйста, уточните/перефразируйте