How can I split a table at given rows?
Afficher commentaires plus anciens
I have an n by m table, A, and a vector of indices, idx.
Is there a way to split my table into smaller tables, each beginning with the row indicated in idx?
For example:
A = ['str1', 'str2';
1, 2;
3, 4;
'str3', 'str4';
5, 6]
idx = [1; 4]
I would like:
A1 = ['str1', 'str2';
1, 2;
3, 4]
A2 = 'str3', 'str4';
5, 6]
Many thanks.
Réponses (3)
Sad Grad Student
le 23 Fév 2015
Try:
A1 = A(idx(1):idx(2)-1,:);
A2 = A(idx(2):end,:);
you can make that into a loop too if you have more values.
funkadelala
le 23 Fév 2015
3 commentaires
Sad Grad Student
le 23 Fév 2015
Modifié(e) : Sad Grad Student
le 23 Fév 2015
I'm not too familiar with table in Matlab. But I believe the error is because you're declaring variable B as a vector (datatype: double) and trying to pass the table in it. Try using: table2array for conversion but since you have strings as your first row, it might give you issues. So along with table2array , look up: Access Data In Table
Some fidgeting around those commands might help you. Let me know what works! :)
funkadelala
le 23 Fév 2015
Modifié(e) : funkadelala
le 23 Fév 2015
Sad Grad Student
le 23 Fév 2015
Cool! Congrats :)
Sad Grad Student
le 23 Fév 2015
Solution to this question:
B = cell(length(idx),1); %preallocates dimensions
for i = 1:1:length(idx)
%creates cell array that contains tables; each cell beginning with
%index idx(i) of table A, ending on index idx(i+1)-1
if i ~= length(idx) %if not last pass
B{i,:} = A(idx(i):idx(i+1)-1,:);
else
B{i,:} = A(idx(i):end,:);
end
end
1 commentaire
Sad Grad Student
le 23 Fév 2015
Had to write it that way so someone having the same question can access the actual working solution! :)
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!