필터 지우기
필터 지우기

Spliting a cell based on the values of a column

조회 수: 1 (최근 30일)
Dibakar Yadav
Dibakar Yadav 2016년 6월 3일
댓글: Dibakar Yadav 2016년 6월 3일
Hi I have a NX2 cell, where the the first column has a data type of the kind int64 and the 2nd column contains strings.
I want to split this cell into smaller cells based on the value of integer in the 1st column. That is if my cell variable is: cell=[1 PB1;3 PB2;1 PBC;2 PB1;1 PBC.......],
I want to create smaller cells out of it; i.e cell_1 which contains all the rows of the original cell which has 1 in their 1st column, cell_2 with 2 in the 1st column and so on . Can this be done without using a for loop, as N can be quite large for me in some cases.
I did it for matrices using logical indexing, but my matrices had only either 1 or2 or 3 or 4 in the 1st column and there I had to specify the value of the elements for carrying out splitting.
Now for the cell array I have values running from 1 upto 290 or more. So,can this be done for cell arrays without exactly specifying what is the value of the element in the column-1 of the cell.
Thanks, Dibakar

채택된 답변

goerk
goerk 2016년 6월 3일
Take the data from column 1 and perform the command
sortInd = unique(column1data);
now you can loop over this values (sortInd) and perform the same as for your matrix solution.

추가 답변 (1개)

Stephen23
Stephen23 2016년 6월 3일
편집: Stephen23 2016년 6월 3일
There is no need to use any loop:
>> C = {1,'PB1';3,'PB2';1,'PBC';2,'PB1';1,'PBC'}
>> [V,idx] = sort([C{:,1}])
>> D = diff([find([1,diff(V)]),1+numel(V)])
>> out = mat2cell(C(idx,:),D,2)
>> out{:}
ans =
[1] 'PB1'
[1] 'PBC'
[1] 'PBC'
ans =
[2] 'PB1'
ans =
[3] 'PB2'

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by