Splitting a table into smaller ones based on two columns
Show older comments
Hello,
The code I use produces a master table with 8 columns and around 800 rows. I would like to sort the data in the following steps:
- Take first line of data from master table and look at the values in column six and eight.
- Find all other rows in the table with a column six value within 0.5 of the line one column six value and a column eight value within 2 of the line one column eight value.
- Create new table with this data.
- Delete sorted data from master table.
- Repeat 1-> 4 until no data left.
- Be left with multiple tables.
Is there a way to do this?
Thank you for your help.
Accepted Answer
More Answers (1)
I would do it as such:
n = 1;
a = your table% I used table([1;2;3;4;5;],[1;5;1;3;1]) to sort of test
while ~isempty(a) % while your table is still empty
% here is finding the index of the logic you did - the second & might be an or (|) if
% you want all the rows that satisfy the col 7 or 8 requirements instead of col 7 AND 8 reqs
% these indices can also more cleanly be done the way adam does.
toRem = find(a.col7<a.col7(1)+0.5 & a.col7>a.col7(1)-0.5 & a.col8<a.col8(1)+2 & a.col8>a.col8(1)-2);
newTables{n} = a(toRem,:); % Put the found values into a new table inside a cell array
% putting them into a cell array is the easiest way to have different sized variables created in a loop
a(toRem,:)=[]; % remove from original table
n=n+1; % increment n
end
All of your resultant tables will now be in newTables.
I agree with Adam that this would not be a great idea in theory, but here is how you could do it if you still really want to sort them this way.
Categories
Find more on Shifting and Sorting Matrices 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!