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

1 view (last 30 days)
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.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 16 Feb 2014
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 Comments
Steven Lord
Steven Lord on 7 Jul 2015
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 on 7 Jul 2015
Thanks Steven,
how I didn't notice that.
many thanks

Sign in to comment.

More Answers (1)

Justin
Justin on 16 Feb 2014
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 Comment
Justin
Justin on 16 Feb 2014
I found I can redefine 'out' into new variables and the number formatting returns.
Thanks again.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!