Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
deleting specific rows

Subject: deleting specific rows

From: Pr B

Date: 16 Jan, 2009 05:32:02

Message: 1 of 5

hi all,

i have a list of numbers as follows (list1):

22
26
51
4
740
...

and a two column list of numbers as follows (list2):

1 13
4 9
22 26
33 740
55 1234

the task i want to accomplish is as follows:
if a number from list1 is found in a row in list2, i would like to remove that row from list2. in the above example the numbers 22, 26, 4, and 740 are found in both columns of list2 and i would like to remove rows 2, 3, and 4 of list2. how would i do this in a short matlab script?

thanks!

Subject: deleting specific rows

From: Pr B

Date: 16 Jan, 2009 05:38:02

Message: 2 of 5

i also have the following code from a prior implementation, but this only works for 2 column arrays. any help would be much appreciated!

kill=[];
for i=1:length(list1)
    if list1(i,1) == list2(i,2)
        kill = [kill i];
    end
end

thanks.

"Pr B" <pb2297@columbia.edu> wrote in message <gkp64i$jeu$1@fred.mathworks.com>...
> hi all,
>
> i have a list of numbers as follows (list1):
>
> 22
> 26
> 51
> 4
> 740
> ...
>
> and a two column list of numbers as follows (list2):
>
> 1 13
> 4 9
> 22 26
> 33 740
> 55 1234
>
> the task i want to accomplish is as follows:
> if a number from list1 is found in a row in list2, i would like to remove that row from list2. in the above example the numbers 22, 26, 4, and 740 are found in both columns of list2 and i would like to remove rows 2, 3, and 4 of list2. how would i do this in a short matlab script?
>
> thanks!

Subject: deleting specific rows

From: Matt Fig

Date: 16 Jan, 2009 05:58:02

Message: 3 of 5

list1 = round(rand(5,1)*10);
list2 = round(rand(8,2)*10)
[idx,idx] = ismember(list2(:,1),list1);
list2(logical(idx),:) = []




j}eW>lcrs}irsjmm%%n_klr`}}KntcC,_}cm__aa}_wm}f}f`mmggc8f_k_

Subject: deleting specific rows

From: Roger Stafford

Date: 16 Jan, 2009 06:05:05

Message: 4 of 5

"Pr B" <pb2297@columbia.edu> wrote in message <gkp64i$jeu$1@fred.mathworks.com>...
> hi all,
>
> i have a list of numbers as follows (list1):
>
> 22
> 26
> 51
> 4
> 740
> ...
>
> and a two column list of numbers as follows (list2):
>
> 1 13
> 4 9
> 22 26
> 33 740
> 55 1234
>
> the task i want to accomplish is as follows:
> if a number from list1 is found in a row in list2, i would like to remove that row from list2. in the above example the numbers 22, 26, 4, and 740 are found in both columns of list2 and i would like to remove rows 2, 3, and 4 of list2. how would i do this in a short matlab script?
>
> thanks!

  tf = reshape(~ismember(list2(:),list1),[],2);
  list2 = list2(tf(:,1)&tf(:,2));

Roger Stafford

Subject: deleting specific rows

From: Jos

Date: 16 Jan, 2009 09:45:03

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gkp82g$b6k$1@fred.mathworks.com>...
> "Pr B" <pb2297@columbia.edu> wrote in message <gkp64i$jeu$1@fred.mathworks.com>...
> > hi all,
> >
> > i have a list of numbers as follows (list1):
> >
> > 22
> > 26
> > 51
> > 4
> > 740
> > ...
> >
> > and a two column list of numbers as follows (list2):
> >
> > 1 13
> > 4 9
> > 22 26
> > 33 740
> > 55 1234
> >
> > the task i want to accomplish is as follows:
> > if a number from list1 is found in a row in list2, i would like to remove that row from list2. in the above example the numbers 22, 26, 4, and 740 are found in both columns of list2 and i would like to remove rows 2, 3, and 4 of list2. how would i do this in a short matlab script?
> >
> > thanks!
>
> tf = reshape(~ismember(list2(:),list1),[],2);
> list2 = list2(tf(:,1)&tf(:,2));
>
> Roger Stafford


You can do without the reshape:
L1 = [22 ; 26 ; 51 ; 4 ; 740]
L2 = [1 13
       4 9
       22 26
       33 740
       55 1234] ;

L2(any(ismember(L2,L1),2),:) = []

hth
Jos

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us