help with matrix concatenation

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

It's unclear what you're trying to do. You say "swap a matrix", but it appears that means "flip a row vector by permuting the individual elements pairwise". If that's the case, then what is "all swapped matrices"? Does that mean you want a array of all said permutations, or do you want an array of various fully flipped vectors?
Hey thanks for getting back to me;i have explained what im trying to do below;
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
Well, let's start with that. This gives the permutation described by the example, as best fits the code provided:
A = [11 12 13; 21 22 23; 31 32 33];
s = size(A);
B = zeros([s(1)^2 s(2)]);
B(1:s(1),:) = A;
for r = 2:s(2)
thisB = A;
thisB(:,[1 r]) = thisB(:,[r 1]);
B((r-1)*s(1)+1:r*s(1),:) = thisB;
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
But while that looks fine for 3 columns, it doesn't generalize. Say A is wider (using a vector for compactness):
A = [1 2 3 4];
% this is unchanged
s = size(A);
B = zeros([s(1)^2 s(2)]);
B(1:s(1),:) = A;
for r = 2:s(2)
thisB = A;
thisB(:,[1 r]) = thisB(:,[r 1]);
B((r-1)*s(1)+1:r*s(1),:) = thisB;
end
B
B = 4×4
1 2 3 4 2 1 3 4 3 2 1 4 4 2 3 1
Which is probably not what you want. There could be other ways to incrementally flip row vectors, but I miss how they would adhere to the examples. What should the incremental permutations look like for a 1x4 or 1x5 vector input?
I would imagine a flip process would look like this:
% initial
1 2 3 4
% first pass
2 1 3 4
3 1 2 4
4 1 2 3
% second pass
4 2 1 3
4 3 1 2
% third pass
4 3 2 1
And the result would be a sample from each pass, say the last sample from each pass
1 2 3 4
4 1 2 3
4 3 1 2
4 3 2 1
The example seems to use the first sample from each pass instead.
1 2 3 % this one
2 1 3 % this one
3 1 2
3 2 1 % this one
Is that what's intended?
Thanks for investing your time to help, the first example is what i was looking for. However, you say it does not work if A is wider and that is also what i was wondering,if it can work if A was winder lets say 7*7,and if it can then how.
DGM
DGM on 9 Oct 2021
Edited: 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

@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

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)

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

If A is 3x3, then B is 9x3. The diagonal of B is the diagonal of the first 3x3 block. The first 3x3 block is a copy of A, so the diagonal of B is the diagonal of A.
B = [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]
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
d = diag(B)
d = 3×1
11 22 33
p = prod(d)
p = 7986
I don't know where this is going
No thats not what i asked, i will try to explain again A=[11 12 13;21 22 23;31 32 33]; B=zeros(1,length(n))
step1--->so the first action will be B=A(i,i)-->B1=prod(B)--->the product of diagonal coefficints in this case (11,22,33)
step2--->then it will swap columns-->column 1 will be column 2 and column 2 will be column 1
A will look the same but columns 1 and 2 will have swapped places
calculate B2 again
step 3--->A will default to its original
then it will swap columns-->column 1 will be column 3 and column 3 will be column 1
then it will calculate B3 again
step4---> this process will continue to the length(A)-1(if matrix was larger)
calculate B(n)
step5---> so taking A as reference,after the process finishesat step 3.that will mean that B has three values since A is a 3*3 matrix.my problem is that i am not able to store the value of of B somewhere after every calculation
this is what i want to happen
step1---find B1
step2---find B2
step2---find B3
Barray=(B1 B2 B3);
i think i am clear now,sorry ive taken so long.
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.

Categories

Products

Release

R2021a

Asked:

on 8 Oct 2021

Edited:

DGM
on 10 Oct 2021

Community Treasure Hunt

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

Start Hunting!