How to make a m*2 matrix into n number of 2x2 matrices

3 visualizaciones (últimos 30 días)
Dyl
Dyl el 7 de Sept. de 2022
Comentada: Stephen23 el 8 de Sept. de 2022
Hey,
I have, A = 208x2 matrix. I wish to to spit this matrix into 104 2x2 matrices. I have tried using num2cell and mat2cell but have had no luck. Any help would be appreicated.
Thanks.

Respuesta aceptada

Stephen23
Stephen23 el 7 de Sept. de 2022
A = rand(208,2);
C = mat2cell(A,2*ones(104,1),2)
C = 104×1 cell array
{2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double}
  6 comentarios
Dyl
Dyl el 8 de Sept. de 2022
Hey I have another question I reshaped the 2x1x104 matrix into a 13x8 matrix where I only took one value for each val(:,:,1) , valu(:,:,2) etc..
13x8 = [-28,-64 ... ;-33,-74...]
I wanted the values to insert into the 13x8 matric column wise so 1-8 for row 1 then 1-8 for row 2 so on, but they inserted row wise. How can I make the matrix I am after? Thanks in advance.

Iniciar sesión para comentar.

Más respuestas (2)

KSSV
KSSV el 7 de Sept. de 2022
A = rand(208,2) ;
[r,c] = size(A);
nlay = 104 ;
out = permute(reshape(A',[c,r/nlay,nlay]),[2,1,3]);

Abderrahim. B
Abderrahim. B el 7 de Sept. de 2022
Split A
A = randi(10, 208, 2) ; % a mtarix of size 208x2
size(A)
ans = 1×2
208 2
B = reshape(A, 2, 2, []) ;
Access 2x2 matrices
B1 = B(:,:,1)
B1 = 2×2
5 7 9 6
B2 = B(:,:,2)
B2 = 2×2
8 8 4 1
Hope this helps
  2 comentarios
Stephen23
Stephen23 el 7 de Sept. de 2022
Editada: Stephen23 el 7 de Sept. de 2022
Note that this method does not keep the 2x2 matrices of the original matrix:
A = randi(10, 208, 2)
A = 208×2
3 1 9 7 9 3 4 9 8 7 5 5 4 4 9 1 8 10 10 8
B = reshape(A, 2,2,[]) % not the same matrices
B =
B(:,:,1) = 3 9 9 4 B(:,:,2) = 8 4 5 9 B(:,:,3) = 8 5 10 4 B(:,:,4) = 2 1 10 10 B(:,:,5) = 2 5 7 9 B(:,:,6) = 4 7 4 9 B(:,:,7) = 6 9 4 6 B(:,:,8) = 10 9 2 3 B(:,:,9) = 10 4 6 4 B(:,:,10) = 8 5 2 5 B(:,:,11) = 4 2 6 4 B(:,:,12) = 5 1 6 8 B(:,:,13) = 7 4 4 6 B(:,:,14) = 3 7 6 6 B(:,:,15) = 4 4 7 9 B(:,:,16) = 9 6 2 10 B(:,:,17) = 8 9 7 6 B(:,:,18) = 3 3 2 4 B(:,:,19) = 6 1 6 3 B(:,:,20) = 4 3 10 10 B(:,:,21) = 5 9 6 5 B(:,:,22) = 2 2 8 10 B(:,:,23) = 4 8 3 7 B(:,:,24) = 9 7 2 3 B(:,:,25) = 8 10 6 9 B(:,:,26) = 6 7 8 4 B(:,:,27) = 7 10 10 1 B(:,:,28) = 3 9 7 4 B(:,:,29) = 4 3 9 10 B(:,:,30) = 4 5 3 2 B(:,:,31) = 2 5 9 5 B(:,:,32) = 2 9 9 4 B(:,:,33) = 2 1 6 9 B(:,:,34) = 8 1 3 4 B(:,:,35) = 2 4 4 3 B(:,:,36) = 7 1 5 4 B(:,:,37) = 10 7 1 10 B(:,:,38) = 8 1 1 10 B(:,:,39) = 10 4 1 8 B(:,:,40) = 2 7 9 7 B(:,:,41) = 3 10 2 7 B(:,:,42) = 8 5 9 5 B(:,:,43) = 3 10 10 1 B(:,:,44) = 7 9 2 3 B(:,:,45) = 2 10 9 10 B(:,:,46) = 8 1 9 6 B(:,:,47) = 5 8 7 9 B(:,:,48) = 2 2 3 2 B(:,:,49) = 3 2 3 5 B(:,:,50) = 4 2 9 3 B(:,:,51) = 7 10 10 3 B(:,:,52) = 9 5 3 3 B(:,:,53) = 1 3 7 9 B(:,:,54) = 7 4 5 1 B(:,:,55) = 10 2 8 10 B(:,:,56) = 7 8 8 4 B(:,:,57) = 3 3 6 1 B(:,:,58) = 9 2 5 6 B(:,:,59) = 10 9 6 3 B(:,:,60) = 1 9 5 4 B(:,:,61) = 5 9 4 3 B(:,:,62) = 7 9 8 5 B(:,:,63) = 3 7 3 10 B(:,:,64) = 8 10 10 8 B(:,:,65) = 6 10 9 4 B(:,:,66) = 8 2 7 3 B(:,:,67) = 3 6 5 3 B(:,:,68) = 7 5 5 6 B(:,:,69) = 3 6 6 10 B(:,:,70) = 4 4 6 1 B(:,:,71) = 2 7 6 8 B(:,:,72) = 1 6 1 5 B(:,:,73) = 7 1 10 1 B(:,:,74) = 1 3 4 6 B(:,:,75) = 1 4 5 9 B(:,:,76) = 10 10 9 1 B(:,:,77) = 10 2 4 4 B(:,:,78) = 4 8 8 3 B(:,:,79) = 10 1 10 8 B(:,:,80) = 6 5 7 9 B(:,:,81) = 10 9 8 5 B(:,:,82) = 5 5 5 8 B(:,:,83) = 1 8 5 1 B(:,:,84) = 10 4 4 8 B(:,:,85) = 8 7 6 4 B(:,:,86) = 4 10 4 2 B(:,:,87) = 9 6 3 2 B(:,:,88) = 9 7 9 7 B(:,:,89) = 10 8 10 7 B(:,:,90) = 1 3 9 4 B(:,:,91) = 7 4 1 8 B(:,:,92) = 10 9 5 9 B(:,:,93) = 2 4 6 8 B(:,:,94) = 6 3 7 10 B(:,:,95) = 10 9 10 2 B(:,:,96) = 10 5 3 2 B(:,:,97) = 10 4 9 4 B(:,:,98) = 10 5 9 8 B(:,:,99) = 5 7 6 7 B(:,:,100) = 7 10 1 9 B(:,:,101) = 1 2 7 9 B(:,:,102) = 8 3 5 6 B(:,:,103) = 10 10 4 8 B(:,:,104) = 6 10 3 4
To keep the original matrices requires taing into account the order of elements stored in memory:
B = permute(reshape(A.',2,2,[]),[2,1,3])
B =
B(:,:,1) = 3 1 9 7 B(:,:,2) = 9 3 4 9 B(:,:,3) = 8 7 5 5 B(:,:,4) = 4 4 9 1 B(:,:,5) = 8 10 10 8 B(:,:,6) = 5 2 4 10 B(:,:,7) = 2 7 10 8 B(:,:,8) = 1 8 10 4 B(:,:,9) = 2 3 7 6 B(:,:,10) = 5 3 9 1 B(:,:,11) = 4 9 4 5 B(:,:,12) = 7 2 9 6 B(:,:,13) = 6 10 4 6 B(:,:,14) = 9 9 6 3 B(:,:,15) = 10 1 2 5 B(:,:,16) = 9 9 3 4 B(:,:,17) = 10 5 6 4 B(:,:,18) = 4 9 4 3 B(:,:,19) = 8 7 2 8 B(:,:,20) = 5 9 5 5 B(:,:,21) = 4 3 6 3 B(:,:,22) = 2 7 4 10 B(:,:,23) = 5 8 6 10 B(:,:,24) = 1 10 8 8 B(:,:,25) = 7 6 4 9 B(:,:,26) = 4 10 6 4 B(:,:,27) = 3 8 6 7 B(:,:,28) = 7 2 6 3 B(:,:,29) = 4 3 7 5 B(:,:,30) = 4 6 9 3 B(:,:,31) = 9 7 2 5 B(:,:,32) = 6 5 10 6 B(:,:,33) = 8 3 7 6 B(:,:,34) = 9 6 6 10 B(:,:,35) = 3 4 2 6 B(:,:,36) = 3 4 4 1 B(:,:,37) = 6 2 6 6 B(:,:,38) = 1 7 3 8 B(:,:,39) = 4 1 10 1 B(:,:,40) = 3 6 10 5 B(:,:,41) = 5 7 6 10 B(:,:,42) = 9 1 5 1 B(:,:,43) = 2 1 8 4 B(:,:,44) = 2 3 10 6 B(:,:,45) = 4 1 3 5 B(:,:,46) = 8 4 7 9 B(:,:,47) = 9 10 2 9 B(:,:,48) = 7 10 3 1 B(:,:,49) = 8 10 6 4 B(:,:,50) = 10 2 9 4 B(:,:,51) = 6 4 8 8 B(:,:,52) = 7 8 4 3 B(:,:,53) = 7 10 10 10 B(:,:,54) = 10 1 1 8 B(:,:,55) = 3 6 7 7 B(:,:,56) = 9 5 4 9 B(:,:,57) = 4 10 9 8 B(:,:,58) = 3 9 10 5 B(:,:,59) = 4 5 3 5 B(:,:,60) = 5 5 2 8 B(:,:,61) = 2 1 9 5 B(:,:,62) = 5 8 5 1 B(:,:,63) = 2 10 9 4 B(:,:,64) = 9 4 4 8 B(:,:,65) = 2 8 6 6 B(:,:,66) = 1 7 9 4 B(:,:,67) = 8 4 3 4 B(:,:,68) = 1 10 4 2 B(:,:,69) = 2 9 4 3 B(:,:,70) = 4 6 3 2 B(:,:,71) = 7 9 5 9 B(:,:,72) = 1 7 4 7 B(:,:,73) = 10 10 1 10 B(:,:,74) = 7 8 10 7 B(:,:,75) = 8 1 1 9 B(:,:,76) = 1 3 10 4 B(:,:,77) = 10 7 1 1 B(:,:,78) = 4 4 8 8 B(:,:,79) = 2 10 9 5 B(:,:,80) = 7 9 7 9 B(:,:,81) = 3 2 2 6 B(:,:,82) = 10 4 7 8 B(:,:,83) = 8 6 9 7 B(:,:,84) = 5 3 5 10 B(:,:,85) = 3 10 10 10 B(:,:,86) = 10 9 1 2 B(:,:,87) = 7 10 2 3 B(:,:,88) = 9 5 3 2 B(:,:,89) = 2 10 9 9 B(:,:,90) = 10 4 10 4 B(:,:,91) = 8 10 9 9 B(:,:,92) = 1 5 6 8 B(:,:,93) = 5 5 7 6 B(:,:,94) = 8 7 9 7 B(:,:,95) = 2 7 3 1 B(:,:,96) = 2 10 2 9 B(:,:,97) = 3 1 3 7 B(:,:,98) = 2 2 5 9 B(:,:,99) = 4 8 9 5 B(:,:,100) = 2 3 3 6 B(:,:,101) = 7 10 10 4 B(:,:,102) = 10 10 3 8 B(:,:,103) = 9 6 3 3 B(:,:,104) = 5 10 3 4
Abderrahim. B
Abderrahim. B el 7 de Sept. de 2022
Thanks @Stephen23. But he does not mention that the order must be te same as in the original matrix!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by