Problem to delete rows in my matrix

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 comentarios

and try
for q = length(res)-1 : -1 : 1
if res(q,3)==res(q+1,3)
res(q,:) = [];
end
end
Roger Stafford
Roger Stafford el 21 de Nov. de 2016
Editada: Roger Stafford el 21 de Nov. de 2016
@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.
Aude Rapet
Aude Rapet el 21 de Nov. de 2016
Thank you for your reply Per! It helps me a lot!

Iniciar sesión para comentar.

 Respuesta aceptada

Roger Stafford
Roger Stafford el 21 de Nov. de 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) = [];

4 comentarios

Ok thank you very much Roger!
The problem I have with my loop is also that it deletes the data I want at the end. I have this :
1592,61638027600 292,462164782916 1 2028
1591,36484205925 292,162090444115 2 2028
1593,62808256023 1584,64135103072 1 2029
1587,97242347533 1584,32241069947 2 2029
1594,59566486772 1371,42195936310 1 2030
1589,67204481414 1371,12117544568 2 2030
595,370627883948 566,978732597834 2 2031
964,731265222265 1166,89283712761 2 2032
1073,40350931937 850,071709928442 2 2033
and I want to keep only the one "paired" with 1-2 in column 3, but with my loop it deletes the last "pair" :
1592,61638027600 292,462164782916 1 2028
1591,36484205925 292,162090444115 2 2028
1593,62808256023 1584,64135103072 1 2029
1587,97242347533 1584,32241069947 2 2029
1594,59566486772 1371,42195936310 1 2030
1073,40350931937 850,071709928442 2 2033
Do you have an idea of how I could keep my "pair" 2030 (column 4), and delete 2031...2033?
Thank you for your reply!! Aude
Aude Rapet
Aude Rapet el 21 de Nov. de 2016
Editada: Aude Rapet el 21 de Nov. de 2016
I wrote this and I think it works but maybe there is an easier way :
for ind = length(res)-1 : -1 : 1
if res(ind,4)== res(ind+1,4)
res_select(ind:ind+1,:) = res(ind:ind+1,:) ;
end
end
for ind = length(res_select)-1 : -1 : 1 %to delete the zeros at the beginning of my new matrix res_select
if res_select(ind,:)==0
res_select(ind,:) = [] ;
end
end
what do you think about it?
Roger Stafford
Roger Stafford el 21 de Nov. de 2016
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?
Aude Rapet
Aude Rapet el 21 de Nov. de 2016
Yes, what I want to do is to select "paired" numbers by my function. So I could either
  1. delete rows from my matrix when two following values in my 3rd column are the same : that means having 1 2 1 2 etc. alternatively in my 3rd column
  2. either keep rows only when two following values in my 4rd (4!!) column are the same, and delete the others rows. With both ways I have the same selected rows at the end.And I think the second option is better, because with the first one I lose the end of my datas...

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 21 de Nov. de 2016

Comentada:

el 21 de Nov. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by