How to remove cells that have the same value at the first row, first column as the previous cell, in a cell array?

조회 수: 2 (최근 30일)
I have tried the following, being "C1" the original cell array and "C1_no_repeats" the cell array that I want to create by deleting the cells of "C1" that have a similar value at (1,1) as the previous cell of "C1":
for aa=1:size(C1)
if C1{aa}(1,1)==C1{aa+1}(1,1)
C1_no_repeats{aa+1}=[];
else C1_no_repeats{aa+1}=C1{aa+1};
end
end
It didn't work: not only C1_no_repeats{1} is empty, but C1_no_repeats stops after the first cell with repeated (1,1) values (if C1_no_repeats has 100 cells and cell 5 has the same values at (1,1) as cell 4, then C1_no_repeats has only 4 cells...)

답변 (1개)

David Hill
David Hill 2022년 2월 28일
There is only one repeated value in the example provided. You could round (1,1) values if you want them to be close but not exactly the same.
b=[];
for aa=1:length(indexed_finalf_before2)
b=[b,indexed_finalf_before2{aa}(1,1)];
end
[~,idx]=unique(b,'stable');
no_repeats=indexed_finalf_before2(idx);

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by