How to break a large table into a set of smaller tables?

2 ビュー (過去 30 日間)
JFz
JFz 2015 年 7 月 14 日
コメント済み: JFz 2015 年 7 月 15 日
Hi,
I have a very large table (priceTbl) of 4 columns. I want to break the large table into a set of smaller tables such that each small table has the same value in column 2 and 4.
How to do that?
Thanks,
Jen

回答 (2 件)

Peter Perkins
Peter Perkins 2015 年 7 月 15 日
Jennifer, it's hard to answer this question without knowing whether "table" means "the MATLAB table data type" or something else, and without knowing what is in the table. Walter's suggestion assumes a table as in "the data type", and further, that variables 2 and 4 in that table are valid inputs to unique(...,'rows'). (Walter, Gold Star for writing working code despite apparently not being able to run it, although you actually only need 13b or later to use tables.) Given that, here's a (perhaps) slightly more direct version of Walter's code:
[~,~,i] = unique(BigTable(:,[2 4]));
for k = 1 : max(i)
TableCell{k} = BigTable(i == k,:);
end
If you're into the whole brevity thing, rowfun lets you do it without a loop:
rowfun(@(x1,x2,x3,x4) table(x1,x2,x3,x4),BigTable, ...
'InputVariables',1:4,'GroupingVariables',[2 4], ...
'OutputFormat','cell')
But Jennifer, given the warning and error that you're getting, it seems likely that either you don't have a table, or the table contains something unusual in those variables. You'll have to provide a short example of your data.
  1 件のコメント
JFz
JFz 2015 年 7 月 15 日
Thank you!
Here, table means Matlab table data type. I will give an example, but first I need to make it generic.
Jennifer

サインインしてコメントする。


Walter Roberson
Walter Roberson 2015 年 7 月 14 日
col24 = BigTable{:,[2 4]};
u24 = unique(col24, 'rows');
numentry = size(u24,1);
TableCell = cell(numentry,1);
for K = 1 : numentry
insubset = BigTable{:,2} == u24(K,1) && BigTable{:,4} == u24(K,2);
TableCell{K} = BigTable(insubset,:);
end
  4 件のコメント
JFz
JFz 2015 年 7 月 15 日
編集済み: JFz 2015 年 7 月 15 日
Thank you so much, Walter! Really appreciate it. But when I tried it, the 2nd line gives me the error:
>> u24 = unique(col24, 'rows'); Warning: The 'rows' input is not supported for cell array inputs. > In cell.unique>celluniqueR2012a at 236 In cell.unique at 148
Do you know what is the problem here?
Also, I got this error: Undefined function 'eq' for input arguments of type 'cell'.
when trying:
insubset = BigTable{:,2} == u24(K,1) && BigTable{:,4} == u24(K,2);
Again, thanks. Really appreciate it. Jennifer
Walter Roberson
Walter Roberson 2015 年 7 月 15 日
Unfortunately I do not have R2014b or later and so cannot test any code that contains tables.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by