3 views (last 30 days)

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

Roger Stafford
on 21 Nov 2016

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) = [];

Roger Stafford
on 21 Nov 2016

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/313183-problem-to-delete-rows-in-my-matrix#comment_407712

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/313183-problem-to-delete-rows-in-my-matrix#comment_407712

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/313183-problem-to-delete-rows-in-my-matrix#comment_407713

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/313183-problem-to-delete-rows-in-my-matrix#comment_407713

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/313183-problem-to-delete-rows-in-my-matrix#comment_407722

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/313183-problem-to-delete-rows-in-my-matrix#comment_407722

Sign in to comment.