hwo to arrange certain rows of a matrix into cell array

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

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 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)

1 Kommentar

Thanks a lot! very brief script and it worked very well with my data.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 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

Kategorien

Produkte

Gefragt:

am 21 Apr. 2014

Kommentiert:

am 21 Apr. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by