help with matrix concatenation
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi Everyone, i am a beginner and i would like to ask if its possible to concatenate more than two matrices,please try and correct my code;
i am trying to swap a matrix then concanate all into one into one big matrice that will contain all swapped matrices so i can access them again.Is that possible and if it is how can i access the matrices later? thank you.
Allmatrix=zeros(length(main),length(main)*length(main));
for i=0:length(main)
for swap=i:length(main)-1
Axb=main;
swaprow=Axb(:,1);
Axb(:,1)=Axb(:,swap+1);
Axb(:,swap+1)=swaprow;
end
Allmatrix=Axb(i);
end
댓글 수: 5
DGM
2021년 10월 9일
편집: DGM
2021년 10월 9일
... That's what I was asking you. You have to define how the process should be generalized to wider arrays. In the last example I gave, there are two implied variations depending on how the loops are structured. For A = [1 2 3 4], you could either get
1 2 3 4 % case 1 (using last sample)
4 1 2 3
4 3 1 2
4 3 2 1
or you could get
1 2 3 4 % case 2 (using first sample)
2 1 3 4
4 2 1 3
4 3 2 1
or you could get something different yet depending on what you intended. This is an arbitrary subsampling of a process by which a vector is flipped by an arbitrary number of pairwise flips. It's not up to me to decide what the goals are.
EDIT:
If the first case meets the requirements, it simplifies very neatly:
A = [1 2 3 4 5 6 7]
s = size(A);
B = zeros([s(1)*s(2) s(2)]);
for rb = 1:s(2)
B((rb-1)*s(1)+1:rb*s(1),:) = A(:,[s(2):-1:(s(2)-rb+2) 1:(s(2)-rb+1)]);
end
B
but this doesn't match the order in your smaller example.
채택된 답변
C B
2021년 10월 9일
@James Kamwela is this what you expect as answer please check
main =[1 2 ; 3 4];
main =
1 2
3 4
Allmatrix=[];
for i=1:size(main,2)
for swap=1:size(main,1)-1
Axb=main;
swaprow=Axb(i,swap);
Axb(i,swap)=Axb(i,swap+1);
Axb(i,swap+1)=swaprow;
end
Allmatrix=[Allmatrix Axb];
end
Allmatrix =
2 1 1 2
3 4 4 3
추가 답변 (2개)
DGM
2021년 10월 9일
I'm just going to post these two examples as an answer.
If you want to sample the process at the end of each pass, the structure is more simple:
A = [1 2 3 4 5 6 7];
s = size(A);
B = zeros([s(1)*s(2) s(2)]);
for rb = 1:s(2)
B((rb-1)*s(1)+1:rb*s(1),:) = A(:,[s(2):-1:(s(2)-rb+2) 1:(s(2)-rb+1)]);
end
B
but this doesn't match the order in your smaller example.
A = [11 12 13; 21 22 23; 31 32 33];
s = size(A);
B = zeros([s(1)^2 s(2)]);
for rb = 1:s(2)
B((rb-1)*s(1)+1:rb*s(1),:) = A(:,[s(2):-1:(s(2)-rb+2) 1:(s(2)-rb+1)]);
end
B
If you want to sample the process at the beginning of each pass, the pattern isn't as neat and the code accordingly isn't as simple.
A = [1 2 3 4 5 6 7];
s = size(A);
B = zeros([s(1)*s(2) s(2)]);
B(1:s(1),:) = A;
B(end-s(1)+1:end,:) = fliplr(A);
for rb = 2:s(2)-1
B((rb-1)*s(1)+1:rb*s(1),:) = A(:,[s(2):-1:(s(2)-rb+3) 2 1 3:(s(2)-rb+2)]);
end
B
but this does match your example...
A = [11 12 13; 21 22 23; 31 32 33];
s = size(A);
B = zeros([s(1)*s(2) s(2)]);
B(1:s(1),:) = A;
B(end-s(1)+1:end,:) = fliplr(A);
for rb = 2:s(2)-1
B((rb-1)*s(1)+1:rb*s(1),:) = A(:,[s(2):-1:(s(2)-rb+3) 2 1 3:(s(2)-rb+2)]);
end
B
댓글 수: 0
James Kamwela
2021년 10월 9일
댓글 수: 3
DGM
2021년 10월 10일
편집: DGM
2021년 10월 10일
Assuming that A is square, then
% these are copied from the main example so i don't ahve to repaste everything
s = [3 3];
B = [11 12 13;21 22 23;31 32 33;12 11 13;22 21 23;32 31 33;13 12 11;23 22 21;33 32 31]
Bd = cellfun(@diag,mat2cell(B,repmat(s(1),[s(2) 1]),s(2)),'uniform',false);
Bd = prod(cell2mat(Bd.'),1)
Those are the products of the diagonals of the 3x3 sub-blocks within B.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!