MATLAB Answers

help with matrix concatenation

3 views (last 30 days)
James Kamwela
James Kamwela on 8 Oct 2021
Edited: DGM on 10 Oct 2021
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 Comments
DGM
DGM on 9 Oct 2021
... 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.

Sign in to comment.

Accepted Answer

Chetan Bhavsar
Chetan Bhavsar on 9 Oct 2021
@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
  1 Comment
James Kamwela
James Kamwela on 9 Oct 2021
Thanks for your help, however this doesn`t fully answer my question.
For example; we have 3*3 matrix A=[a1 a2 a3;b1 b2 b3;c1 c2 c3];
after swapping and concatenation A should look like
A=[a1 a2 a3;b1 b2 b3;c1 c2 c3;a2 a1 a3;b2 b1 b3;c2 c1 c3;a3 a2 a1;b3 b2 b1;c3 c2 c1];
i hope you can read the matrix and i hope i make sense

Sign in to comment.

More Answers (2)

DGM
DGM on 9 Oct 2021
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
B = 7×7
1 2 3 4 5 6 7 7 1 2 3 4 5 6 7 6 1 2 3 4 5 7 6 5 1 2 3 4 7 6 5 4 1 2 3 7 6 5 4 3 1 2 7 6 5 4 3 2 1
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
B = 9×3
11 12 13 21 22 23 31 32 33 13 11 12 23 21 22 33 31 32 13 12 11 23 22 21 33 32 31
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
B = 7×7
1 2 3 4 5 6 7 2 1 3 4 5 6 7 7 2 1 3 4 5 6 7 6 2 1 3 4 5 7 6 5 2 1 3 4 7 6 5 4 2 1 3 7 6 5 4 3 2 1
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
B = 9×3
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

James Kamwela
James Kamwela on 9 Oct 2021
so i guess it doesnt continue after the third swap,So now that i cant concatenate more than 3 matrices i would like to ask another question, lets suppose A = [11 12 13; 21 22 23; 31 32 33]; we swap each column with column one however after the swap i want the product of the diagonal stored in B, so that should make B a (1,3) vector,
this is actually what i was struggling with, i am able to swap the columns but B only stores the value of the last swap diagonal product, so what i think happens is that it finishes all the swaps then calculates the diagonal product of the last matrix
i hope i made sense and thank you for your help
  3 Comments
DGM
DGM on 10 Oct 2021
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]
B = 9×3
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)
Bd = 1×3
7986 8316 8866
Those are the products of the diagonals of the 3x3 sub-blocks within B.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!