필터 지우기
필터 지우기

Need to split matrix(m x n) where n varies

조회 수: 3 (최근 30일)
Justin
Justin 2014년 2월 16일
댓글: rewayda mohsin 2015년 7월 7일
Hello everyone,
I have read a lot about splitting matrices and have come close to my intended output but I'm still not there. My matrix is size (7745 x 13 double) and I would like to create 2 new datasets. The number of datasets produced will vary depending on the number of columns in the matrix(which will depend on the number of objects imported):
7 cols = 1 dataset
13 cols = 2 datasets
19 cols = 3 datasets
etc...
Each dataset needs to have 7 cols of data(the 1st col will be reproduced in each dataset followed by the next 6 cols):
Dataset 1 = matrix(cols_1-7
Dataset 2 = matrix(col_1, cols_8-13)
Dataset 3 = matrix(col_1, cols_14-19)
etc...
Given that the matrix dimension 'n' will vary by a value of 6 depending on the number of objects imported I would like the code to be dynamic. I know I can do this interactively but I will also be working with big data and would like a programatic solution.I am not sure if this is possible, efficient or otherwise. I will need to access each column individually and know what "group" of 7 vectors it belongs to, thats why I am thinking datasets, maybe struct would be better.
Thanks for you help,
Please let me know if I can provide any other info to help with the solution.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 16일
M=rand(7745,13);
[n,m]=size(M);
m=m-1;
c1=M(:,1);
mm=fix(m/6)*6;
m1=m-mm;
M(:,end+1:end+mod(6-m1,6))=nan;
A=M(:,2:end);
B=reshape(A,n,6,[]);
[n,m,p]=size(B);
out=zeros(n,m+1,p);
for k=1:size(B,3)
out(:,:,k)=[c1 B(:,:,k)];
end
  댓글 수: 3
Steven Lord
Steven Lord 2015년 7월 7일
The statement inside the FOR loop does not end in a semicolon, so the "many" outputs you're seeing are the out array being displayed at each iteration. Add the semicolon to the "out(:, :, k) = ..." line, then display the variable named out after the FOR loop is complete.
rewayda mohsin
rewayda mohsin 2015년 7월 7일
Thanks Steven,
how I didn't notice that.
many thanks

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

추가 답변 (1개)

Justin
Justin 2014년 2월 16일
Thanks for the quick response. How can I maintain number formatting? See below.
Sample Desired Output (Dataset 1):
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3477e+005 NaN NaN NaN NaN NaN NaN
7.3478e+005 NaN NaN NaN NaN NaN NaN
7.3478e+005 NaN NaN NaN NaN NaN NaN
7.3478e+005 101.85 102.75 100.78 101.71 1000 742
7.3478e+005 101.16 102.28 99.17 99.79 976 736
7.3478e+005 101.95 102.8 100.8 102.73 539 736
7.3478e+005 103.3 105.84 101.59 105.73 706 732
7.3478e+005 105.4 106.55 104.41 105.88 699 791
Same Sample Using the code provided:
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3477 NaN NaN NaN NaN NaN NaN
7.3478 NaN NaN NaN NaN NaN NaN
7.3478 0.0010 0.0010 0.0010 0.0010 0.0100 0.0074
7.3478 0.0010 0.0010 0.0010 0.0010 0.0098 0.0074
7.3478 0.0010 0.0010 0.0010 0.0010 0.0054 0.0074
7.3478 0.0010 0.0011 0.0010 0.0011 0.0071 0.0073
7.3478 0.0011 0.0011 0.0010 0.0011 0.0070 0.0079
  댓글 수: 1
Justin
Justin 2014년 2월 16일
I found I can redefine 'out' into new variables and the number formatting returns.
Thanks again.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by