フィルターのクリア

Use cells rather than matrices to sort a matrix into 255 separate lists

2 ビュー (過去 30 日間)
Rebecca
Rebecca 2012 年 7 月 11 日
I am trying to write a program that will sort an nx5 matrix into 255 separate lists (and it was suggested that I use cells to do this). n is usually upwards of 600,000. The numbers in the 5th column range from 1 to 255 (all are repeated), and they identify which basket the row belongs in. I am unfamiliar with how cells work, but since their size does not need to be pre-allocated, someone said this would be a good tool to use. I would like to label the baskets from 1 to 255, basket(i), or something to that effect, but I don't know the correct syntax to do that. "flag" and "switch" were also suggested. Would it be best to loop through the entire nx5 matrix and sort the rows into baskets that way? Any help with this would be appreciated. Thank you!

採用された回答

Kye Taylor
Kye Taylor 2012 年 7 月 11 日
If I understand your question, the following may help
% create array like yours... n-by-5, last column has index of "bin" which
% will be in the set {1,2,...,maxIndex}
n = 10;
maxIndex = 5;
C = [rand(n,4),randi(maxIndex,n,1)];
% idx is a n-by-1 cell array where idx{j} contains the indices of rows
% belonging to bin j
idx = accumarray(C(:,5), 1:size(C,1),[],@(C){C});
% collect those rows from the matrix belonging to bin j into the jth cell
% of bins
bins = cellfun(@(x)C(x,:),idx,'uniformoutput',false);
  2 件のコメント
Kye Taylor
Kye Taylor 2012 年 7 月 11 日
If only accumarray could take a cell array as its second input... wouldn't that be sweet.
Rebecca
Rebecca 2012 年 7 月 11 日
Thank you so much! This is exactly what I needed.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by