hwo to arrange certain rows of a matrix into cell array

1 view (last 30 days)
Hello!
I need to arrange certain rows of a matrix into a cell array. The data matrix is arranged as follows: [positionx positiony time ID] I would like to place in array{i} all the rows that share the same ID.
Anybody has an idea how to do that?
Thanks, Jack

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 21 Apr 2014
A=[1 2 0 12;2 3 1 54;7 8 2 12;2 10 3 12;0 1 4 54;1 1 4 77]
[ii,jj,kk]=unique(A(:,4))
out=accumarray(kk,1:numel(kk),[],@(x) {A(x,:)})
celldisp(out)

More Answers (1)

the cyclist
the cyclist on 21 Apr 2014
Here's one way:
% Some pretend data
data = [1 2 3 1; ...
4 5 6 2; ...
7 8 9 1];
% Identify the unique IDs, and indices to them
[uniqueId,~,indexFromUniqueBackToAll] = unique(data(:,4));
% Convenient to define the number of unique ids
numberUniqueIds = numel(uniqueId);
% Preallocate memory for the cell array
cellData = cell(numberUniqueIds,1);
% For each unique id, fill in the cell array
for nu = 1:numberUniqueIds
indexToThisId = (indexFromUniqueBackToAll==nu);
cellData{nu} = data(indexToThisId,:);
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!