Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB: deleting rows

  1. Jan 15, 2009 #1
    hi all,

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


    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?


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

    for i=1:length(list1)
    if list1(i,1) == list2(i,2)
    kill = [kill i];
  2. jcsd
  3. Jan 16, 2009 #2


    User Avatar
    Gold Member

    I'm of no help to you on Matlab but I thought I'd show a Python alternative on the chance that you will have the option to use in the future. Python plus its plugin packages numpy and scipy are increasing used for scientific computing and they're free.

    The third line here is called a http://www.python.org/dev/peps/pep-0202/" [Broken] in python
    Code (Text):
    l1=[22, 26, 51, 4, 740]
    l2=[[1, 13], [4, 9], [22, 26], [33, 740], [55, 1234]]
    [row for row in l2 if row[0] not in l1 and row[1] not in l1]

    [B][[1, 13], [55, 1234]] [/B]
    or one can break out the list in to more familia for - if - repeat iterations on separate lines. In either case the python run time machine has to iterate over the list (as does Matlab IIR). Numpy/Scipy provide array/vector methods in which the iteration is done in compiled C code and is thus much faster.
    http://www.scipy.org/SciPy [Broken]
    Last edited by a moderator: May 3, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook