help with matrix concatenation

4 次查看(过去 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 个评论
James Kamwela
James Kamwela 2021-10-9
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 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
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
  1 个评论
James Kamwela
James Kamwela 2021-10-9
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

请先登录,再进行评论。

更多回答(2 个)

DGM
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
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 2021-10-9
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 个评论
James Kamwela
James Kamwela 2021-10-10
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.
DGM
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]
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by