help with matrix concatenation
Mostrar comentarios más antiguos
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 comentarios
DGM
el 9 de Oct. de 2021
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?
James Kamwela
el 9 de Oct. de 2021
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
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
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?
James Kamwela
el 9 de Oct. de 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.
Respuesta aceptada
Más respuestas (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
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
James Kamwela
el 9 de Oct. de 2021
0 votos
3 comentarios
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]
d = diag(B)
p = prod(d)
I don't know where this is going
James Kamwela
el 10 de Oct. de 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]
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.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!