How to Remove Rows from a Two Column List in MATLAB: Step-by-Step Guide

  • Context: MATLAB 
  • Thread starter Thread starter hoffmann
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 7K views
hoffmann
Messages
65
Reaction score
0
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!

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
 
Physics news on Phys.org
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/" in python
Code:
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 into 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
 
Last edited by a moderator: