Break a big matrix into cell array based on repetitive number in one of the matrix columns
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mori
am 24 Jul. 2016
Kommentiert: Star Strider
am 24 Jul. 2016
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155092/image.png)
I have a matrix of 383533*3 doubles, column 2 includes some repetitive numbers, like 2.5483e+06 here, which is similar for 600 raw. how I can break down this big matrix into cells which each cell includes 3 columns of numbers which second column is constant for each cell.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 24 Jul. 2016
This approach works:
M = randi(9, 20, 3); % Create Data
M(1:9,2) = ones(9,1)*3; % Create Data
M(10:end,2) = ones(11,1)*7; % Create Data
[U2,ia,ic] = unique(M(:,2)); % Unique Values In Column #2
rows = accumarray(ic, 1); % Lengths Uf Unique Values
Result = mat2cell(M, rows, size(M,2)); % Cell Array Of Separated Matrices
2 Kommentare
Weitere Antworten (1)
Azzi Abdelmalek
am 24 Jul. 2016
%------Example---------------
A(1:1000,[1 3])=randi(100,1000,2)
A(:,2)=randi(3,1000,1)
%-----The code-----------------
[ii,jj,kk]=unique(A(:,2))
out=accumarray(kk,(1:numel(kk))',[],@(x) {A(x,:)})
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!