Delete specific rows from a cell array and create a new cell array
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello Everyone,
I have created a 1x8 cell array from a text file. I want to create a new cell array that will only have a particular feature. If my cell array is "S" then I want to use a condition on S{8}. if any element (n) of S{8} is greater than its next element (n+1) I want to delete the entire row and after deleting all the unwanted rows, I want to create a new cell array with the remaining elements.
For example:
if I have these elements in my cell array "S"
0,0,1,0,0,0,0,10761
0,0,1,0,0,0,0,11067
0,0,1,0,0,0,0,3036687367
0,0,1,0,0,0,0,11271
.
.
0,0,0,0,0,0,0,28509
0,0,0,0,0,0,0,1656057693
0,0,0,0,0,0,0,28713
0,0,0,0,0,0,0,28815
I want to create a new cell array "T" that will only have these elements:
0,0,1,0,0,0,0,10761
0,0,1,0,0,0,0,11067
0,0,1,0,0,0,0,11271
.
.
0,0,0,0,0,0,0,28509
0,0,0,0,0,0,0,28713
0,0,0,0,0,0,0,28815
Any Suggestions?
Thanks.
댓글 수: 0
답변 (1개)
dpb
2019년 12월 13일
Place where converting the cell array to regular double array makes sense it would seem...there's nothing in the above data that isn't numeric so just use the native double array.
S=cell2mat(S); % convert to double array
ix=[false; (diff(S(:,8))>0)]; % locations of +-ive difference
S(ix,:)=[]; % remove those rows
댓글 수: 5
dpb
2019년 12월 14일
"Here you can see that the code has also skipped some data points (in S{1,8} Row :42 is now Row: 3 in T matrix) and many more."
No, we can't see anything of the sort. What does "the code has skipped some data points" mean? It deleted everything for which diff(S)>0 as per the original specification--that there are duplicated values and therefore a set of points for which the difference is now zero isn't anything that was given as a requirement.
Would have to see all the original data to know; perhaps the condition should be
ix=[false; (diff(T(:,8))>=0)];
but can't tell without a definitive statement of what is the desired result; some heuristic that may work for a given dataset isn't a specification.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!