Need to split matrix(m x n) where n varies
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Justin
le 16 Fév 2014
Commenté : rewayda mohsin
le 7 Juil 2015
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 commentaires
Réponse acceptée
Azzi Abdelmalek
le 16 Fév 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 commentaires
Steven Lord
le 7 Juil 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.
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!