필터 지우기
필터 지우기

Combine arrays to form another arrays

조회 수: 1 (최근 30일)
Ellina
Ellina 2014년 9월 9일
댓글: Andrei Bobrov 2014년 9월 9일
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
  댓글 수: 1
Pierre Benoit
Pierre Benoit 2014년 9월 9일
What is the relation between the number of arrays you wish to concatenate each time and k ? Is it always 4, or k-1 or something else ?

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

채택된 답변

Guillaume
Guillaume 2014년 9월 9일
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
  댓글 수: 1
Ellina
Ellina 2014년 9월 9일
It's work. Thank you so much. :-)

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

추가 답변 (1개)

Pierre Benoit
Pierre Benoit 2014년 9월 9일
편집: Pierre Benoit 2014년 9월 9일
M = 5;
N = 5;
k = 5;
H = randn(M, N, k) + j*randn(M, N, k);
permut = nchoosek(1:k,k-1); % Find all permutations
permut = permut(end:-1:1,:); % Flip array
Hi = reshape(permute(reshape(H(:,:,permut'),[M N k-1 k]),[1 3 2 4]),
[M*(k-1) N k]);
Or with a for-loop to be more explicit
Hi = zeros(M*(k-1),N,k); % Pre-allocation
for ii = 1:k-1
Hi(:,:,ii) = reshape(permute(H(:,:,permut(ii,:)),[1 3 2]),[M*(k-1) N]);
end
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2014년 9월 9일
variant
M = 4;
N = 3;
k = 5;
H = randn(M, N, k) + 1i*randn(M, N, k);
p = flipud(nchoosek(1:k,k-1));
Tout = permute(reshape(permute(H(:,:,p),[1 3 2]),[],k-1,N),[1 3 2]);

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

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by