Concatenate I x J*2 x K matrix in I*J x 2 x K matrix

1 Ansicht (letzte 30 Tage)
Juan Pablo
Juan Pablo am 3 Dez. 2019
Kommentiert: Juan Pablo am 3 Dez. 2019
Hi Guys,
I was using this code line for concatenate a [8 x 4 x 6] matrix in a [16 x 2 x 6] matrix:
X = [x(:,1:2,:); x(:,3:4,:)];
M1 (2).png
Now, I am trying to convert a [200 x 200 x 11] matrix in a [40000 x 2 x 11] matrix, using the code line before mentioned:
X = [x(:,1:2,:); x(:,3:4,:); x(:,5:6,:); x(:,7:8,:); x(:,9:10,:); x(:,11:12,:); x(:,13:14,:); x(:,15:16,:); x(:,17:18,:); x(:,19:20,:); ...
(and so on)
x(:,181:182,:); x(:,183:184,:); x(:,185:186,:); x(:,187:188,:); x(:,189:190,:); x(:,191:192,:); x(:,193:194,:); x(:,195:196,:); x(:,197:198,:); x(:,199:200,:)];
It works, but I can do the same with a for loop (or another approach), and for general matrix array?
[I x J*2 x K] to [I*J x 2 x K]
Thanks for your help in advance.

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Dez. 2019
Bearbeitet: Stephen23 am 3 Dez. 2019
Use reshape and permute, e.g. for two columns:
>> A = randi(9,8,4,6); % fake data
>> A(:,:,1)
ans =
9 1 4 6
7 9 7 7
5 6 3 4
6 5 3 5
2 5 3 6
7 2 5 9
1 5 5 2
5 6 4 1
>> B0 = [A(:,1:2,:);A(:,3:4,:)]; % your method for comparison
>> [R,~,P] = size(A);
>> B1 = reshape(permute(reshape(A,R,2,[],P),[1,3,2,4]),[],2,P);
And checking:
>> isequal(B0,B1)
ans = 1
>> B0(:,:,1)
ans =
9 1
7 9
5 6
6 5
2 5
7 2
1 5
5 6
4 6
7 7
3 4
3 5
3 6
5 9
5 2
4 1
>> B1(:,:,1)
ans =
9 1
7 9
5 6
6 5
2 5
7 2
1 5
5 6
4 6
7 7
3 4
3 5
3 6
5 9
5 2
4 1
An example with six columns:
>> A = randi(9,3,6,5); % fake data
>> A(:,:,1)
ans =
2 3 8 8 2 6
5 5 1 5 6 7
2 2 1 8 1 1
>> B0 = [A(:,1:2,:);A(:,3:4,:);A(:,5:6,:)]; % your method for comparison
>> [R,~,P] = size(A);
>> B1 = reshape(permute(reshape(A,R,2,[],P),[1,3,2,4]),[],2,P);
And checking:
>> isequal(B0,B1)
ans = 1
>> B0(:,:,1)
ans =
2 3
5 5
2 2
8 8
1 5
1 8
2 6
6 7
1 1
>> B1(:,:,1)
ans =
2 3
5 5
2 2
8 8
1 5
1 8
2 6
6 7
1 1

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by