MATLAB: Combine arrays to form another arrays

arraysmatrix array

Hello, can we combine many arrays into another arrays?
for example:
k = 5;
for u=1:k
H(:,:,u)=randn(M,N)+j*randn(M,N);
end
for u=1:k
Hi(:,:,1)=[H(:,:,2);H(:,:,3);H(:,:,4);H(:,:,5)];
Hi(:,:,2)=[H(:,:,1);H(:,:,3);H(:,:,4);H(:,:,5)];
Hi(:,:,3)=[H(:,:,1);H(:,:,2);H(:,:,4);H(:,:,5)];
Hi(:,:,4)=[H(:,:,1);H(:,:,2);H(:,:,3);H(:,:,5)];
Hi(:,:,5)=[H(:,:,1);H(:,:,2);H(:,:,3);H(:,:,4)];
end
what if k=19, should I repeat all the steps. Are there other simpler programs to combine it. Thank you

Best Answer

  • H = randn(M, N, k) + j*randn(M, N, k); %no need for loop
    c = num2cell(H, [1 2]); %split H into cell array along 3rd dimension
    for u=1:k
    indices = [1:u-1 u+1:k]; %get numbers 1:k without u
    Hi(:, :, u) = vertcat(c{indices}); %use cell array to comma separated list expansion to feed vertcat
    end