-2

I have an array Z=[x,y] in matlab and i want to remove zero values of y and also the corresponding x. I also want to remove the descending trends. help me Z =

      0         0
   11.0000         0
   15.0000    4.1667
   23.0000    4.1667
   25.0000    2.7778
   28.0000         0
   49.0000         0
   54.0000    4.1667
   56.0000    4.1667
   61.0000    8.8889
   85.0000    8.8889
   93.0000    2.7778
   96.0000         0
  117.0000         0
  122.0000    4.1667
  124.0000    4.1667
  133.0000    9.7222
  135.0000    9.7222
  143.0000   13.8889
  155.0000   13.8889
  163.0000    9.7222
  176.0000    9.7222
  178.0000    8.8889
  185.0000    2.7778
  188.0000         0
  195.0000         0
  • What do you mean by "descending trend in the 2nd column"? Can you please explain it with this example? – Luis Mendo Jan 08 '14 at 22:56
  • I want to remove the points from which it starts decreasing to which it ends. I think it happens thrice in this array. – user3174907 Jan 08 '14 at 23:07
  • Please be as clear as possible. In the final `Z` of P0W's response, would you want to remove rows 2 3 7 8 14 15 16 17 18? (That's what I understand as "decreasing trend") – Luis Mendo Jan 08 '14 at 23:22

2 Answers2

1

Using all

Your condition is:

all(Z(:,2)==0,2) all 2nd column should be zero

Then , finally

Z(all(Z(:,2)==0,2),:)=[] will give you the expected result, i.e. remove the entire row

Z =[     0         0   ;
   11.0000         0   ;
   15.0000    4.1667   ;
   23.0000    4.1667   ;
   25.0000    2.7778   ;
   28.0000         0   ;
   49.0000         0   ;
   54.0000    4.1667   ;
   56.0000    4.1667   ;
   61.0000    8.8889   ;
   85.0000    8.8889   ;
   93.0000    2.7778   ;
   96.0000         0   ;
  117.0000         0   ;
  122.0000    4.1667   ;
  124.0000    4.1667   ;
  133.0000    9.7222   ;
  135.0000    9.7222   ;
  143.0000   13.8889   ;
  155.0000   13.8889   ;
  163.0000    9.7222   ;
  176.0000    9.7222   ;
  178.0000    8.8889   ;
  185.0000    2.7778   ;
  188.0000         0   ;
  195.0000         0   ]

>> Z(all(Z(:,2)==0,2),:)=[]

Z =

   15.0000    4.1667
   23.0000    4.1667
   25.0000    2.7778
   54.0000    4.1667
   56.0000    4.1667
   61.0000    8.8889
   85.0000    8.8889
   93.0000    2.7778
  122.0000    4.1667
  124.0000    4.1667
  133.0000    9.7222
  135.0000    9.7222
  143.0000   13.8889
  155.0000   13.8889
  163.0000    9.7222
  176.0000    9.7222
  178.0000    8.8889
  185.0000    2.7778
P0W
  • 46,614
  • 9
  • 72
  • 119
  • Thank you very much for the help. One more thing dude, I also want to remove the descending trend in the second column and remove the respective 1st column value. – user3174907 Jan 08 '14 at 20:40
1

First part (remove rows where 2nd column is zero): as in @P0W's answer, but all is not needed:

Z(Z(:,2)==0,:) = [];

Second part (remove rows which have a descending trend in 2nd column): use diff:

ind = find([diff(Z(:,2))]<0);
Z(union(ind,ind+1),:) = [];

I am considering "descending trend" as any group of consecutive rows with decreasing values in the 2nd column.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147