Problem to delete rows in my matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I am a beginner in matlab and I try to delete rows from my matrix when two following values in my 3rd column are the same. I tried this :
for q=1:length(res)-1
if res(q,3)==res(q+1,3)
res(q,:) = [];
end
end
But I have the error message :
Index exceeds matrix dimensions.
Error in trackthebeads (line 8)
if res(q,3)==res(q+1,3)
I had 4020 values at the beginning, it managed to delete 36 but then it blocked I don't know why. Can you help me? Thanks, Aude
댓글 수: 3
Roger Stafford
2016년 11월 21일
편집: Roger Stafford
2016년 11월 21일
@Per. I think this method might have the difficulty that a row might be deleted which might have provided a match in column 3 on the next step down, and that would abort another valid deletion. That is to say, there might be three successive equal elements in column 3 but only one row deletion as a result instead of two.
@Per Correction: On second thought I think your revised method actually does work properly. Forget the above paragraph.
채택된 답변
Roger Stafford
2016년 11월 21일
The problem here is that you are reducing the row count of ‘res’ whenever you get a successive pair in column 3 that match. Hence when q gets near its upper end ‘res’ is no longer as large as it was initially.
One way of proceeding would be to simply collect all the row indices that need to be deleted, and then afterwards delete them all at once:
d = [];
for q=1:length(res)-1
if res(q,3)==res(q+1,3)
d = [d,q];
end
end
res(d) = [];
댓글 수: 4
Roger Stafford
2016년 11월 21일
That would appear to violate the rule that you originally stated: “delete rows from my matrix when two following values in my 3rd column are the same”. Can you carefully restate your criterion for deleting rows so as to include the case you mentioned here?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!