How do i reshape this matrix?

13 vues (au cours des 30 derniers jours)
Joseph Lister-Symonds
Joseph Lister-Symonds le 13 Nov 2020
Hello all, I'm looking for some help on the job below where I am simply trying to reshape a matrix. The data im working with is real, but i have used syms below to hopefully make it a bit easier to read.
syms a11 b11 c11 a12 b12 c12 a21 b21 c21 a22 b22 c22 a13 b13 c13 a23 b23 c23
X=[a11 b11 c11 a12 b12 c12 a13 b13 c13;
a21 b21 c21 a22 b22 c22 a23 b23 c23]
% Dimensions of new matrix M
rows=size(X,1)*size(X,2)/3
cols=3
% ----------- My attempt
for i=1:rows
for j=1:size(X,1)
for k=1:3:(size(X,2))
M(i,1:cols)=X(j,k:(k+2))
end
end
end
The result from my attempt is not what im after at all!
M =
[ a23, b23, c23]
[ a23, b23, c23]
[ a23, b23, c23]
[ a23, b23, c23]
[ a23, b23, c23]
[ a23, b23, c23]
How do I get the code to position each set of three elements (a,b,c) from X sequentially in matrix M like below?
M=[a11 b11 c11;
a12 b12 c12;
a13 b13 c13;
a21 b21 c21;
a22 b22 c22;
a23 b23 c23]

Réponse acceptée

Cris LaPierre
Cris LaPierre le 13 Nov 2020
Modifié(e) : Cris LaPierre le 13 Nov 2020
I'll do it with a numeric matrix. I've replaced a,b and c with a leading 1,2 and 3.
X=[111 211 311 112 212 312 113 213 313
121 221 321 122 222 322 123 223 323];
M=reshape(X',[3,6])'
M = 6×3
111 211 311 112 212 312 113 213 313 121 221 321 122 222 322 123 223 323
  1 commentaire
Joseph Lister-Symonds
Joseph Lister-Symonds le 13 Nov 2020
perfect, thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by