Need to split matrix(m x n) where n varies
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!