hwo to arrange certain rows of a matrix into cell array
Ältere Kommentare anzeigen
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
Weitere Antworten (1)
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
1 Kommentar
jack
am 21 Apr. 2014
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!