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
Click For Summary
SUMMARY

This discussion focuses on removing rows from a two-column list in MATLAB based on the presence of numbers from a separate list. The user seeks a MATLAB script to eliminate rows from list2 if any number from list1 appears in either column. The provided MATLAB code snippet is ineffective for this task, as it only checks one column. A Python alternative using list comprehensions is also mentioned, demonstrating a more efficient approach.

PREREQUISITES
  • Basic understanding of MATLAB syntax and operations
  • Familiarity with array manipulation in MATLAB
  • Knowledge of Python list comprehensions
  • Understanding of scientific computing concepts
NEXT STEPS
  • Research MATLAB logical indexing techniques
  • Learn about MATLAB's ismember function for array comparisons
  • Explore Python's NumPy library for efficient array operations
  • Investigate performance differences between MATLAB and Python for scientific computing tasks
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and programmers looking to optimize data manipulation tasks, as well as those interested in comparing MATLAB with Python for scientific computing applications.

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:

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K