How can I convert a 2D matrix into separate 1D vectors in one line

조회 수: 4 (최근 30일)
Amir Patel
Amir Patel 2016년 12월 19일
댓글: Amir Patel 2016년 12월 19일
hi
I have a 1673 x 34 matrix of data. I would like to put each column into its own 1673 length variable.
I know I can index each of these one at a time, but it will take long with 34 variables. Is there a way to do it in one line?
I can do it with a 1 x 34 using num2cell and then deal. Eg:
states = num2cell(states);
[ ddx, ddy, ddz, ddphi1, ddth1, ddpsi1, ddth3, ddpsi3, ddth4, ddpsi4, dx, dy, dz, dphi1, dth1, dpsi1, dth2, dth3, dpsi3, dth4, dpsi4, x, y, z, phi1, th1, psi1, th2, th3, psi3, th4, psi4, k, b]=deal(states{:});

채택된 답변

Star Strider
Star Strider 2016년 12월 19일
Please don’t!
I would use the mat2cell function to create them as a cell array. It is easier to address them as individual cell elements than as columns of your matrix.
Example:
M = randi(9, 1673, 34); % Create Data
C = mat2cell(M, size(M,1), ones(1,size(M,2))); % Create Column-Vector Cells
  댓글 수: 3
Amir Patel
Amir Patel 2016년 12월 19일
Thanks for the fast reply.
How would I then be able to assign them to individual variables? Could I still use the "deal" function?
Amir Patel
Amir Patel 2016년 12월 19일
Nevermind! Matt J's solution worked :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Coordinate Transformations and Trajectories에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by