# Thread Subject: How to remove multiple values from array

 Subject: How to remove multiple values from array From: Peter Date: 7 May, 2012 04:28:05 Message: 1 of 12 For a given array of data I'd like to remove multiple values specified in another array. e.g. a=1:10 b=[3 6] =>remove b from a result c=[ 1 2 4 5 7 8 9 10] I've looked at the any() command a(any(a=3))=[] will remove the 3 but I haven't found a way to remove an array of values without looping over the length of b. I know it all comes down to loops in the end, but for the size of data I thought Matlab might have an optimized routine for this - maybe one that would break up for multithreading instead of my one loop. Thanks for the ideas, Peter
 Subject: How to remove multiple values from array From: Nasser M. Abbasi Date: 7 May, 2012 04:47:19 Message: 2 of 12 On 5/6/2012 11:28 PM, Peter wrote: > For a given array of data I'd like to remove multiple values specified >in another array. e.g. > > a=1:10 > b=[3 6] > =>remove b from a > result c=[ 1 2 4 5 7 8 9 10] > > I've looked at the any() command a(any(a=3))=[] will remove >the 3 but I haven't found a way to remove an array of values without >looping over the length of b. I know it all comes down to loops >in the end, but for the size of data I thought Matlab might have an >optimized routine for this - maybe one that would break up for >multithreading instead of my one loop. > > Thanks for the ideas, > Peter you'r right, I could not find a way to do it now, will make coeffee and try again. But you could always use arrayfun(). I know it is like a loop, but at least it is more functional style and hide all the loop details away from the user and easy to use: ---------------------- clear all; a = 1:10 b = [3 6]; a(arrayfun(@(i) a(a==b(i)),1:length(b)))=[] ------------------------       1 2 4 5 7 8 9 10 good question. --Nasser --Nasser
 Subject: How to remove multiple values from array From: dpb Date: 7 May, 2012 05:03:06 Message: 3 of 12 On 5/6/2012 11:28 PM, Peter wrote: > For a given array of data I'd like to remove multiple values specified > in another array. e.g. > > a=1:10 > b=[3 6] > =>remove b from a > result c=[ 1 2 4 5 7 8 9 10] ... > the size of data I thought Matlab might have an optimized routine for > this - maybe one that would break up for multithreading instead of my > one loop. ... Couple of options depending on what want to do w/ a...  >> a=1:10;  >> a(intersect(a,b))=[] a =       1 2 4 5 7 8 9 10  >> c=setdiff(a,b) c =       1 2 4 5 7 8 9 10  >> --
 Subject: How to remove multiple values from array From: Bruno Luong Date: 7 May, 2012 06:19:07 Message: 4 of 12 "Nasser M. Abbasi" wrote in message ... > > a = 1:10 > b = [3 6]; > a(arrayfun(@(i) a(a==b(i)),1:length(b)))=[] > ------------------------ arrayfun solution is slow, complex, and even not working with  a = [1:10 1:10] Bruno
 Subject: How to remove multiple values from array From: Bruno Luong Date: 7 May, 2012 06:23:06 Message: 5 of 12 dpb wrote in message ... > >> a=1:10; > >> a(intersect(a,b))=[] > a = > 1 2 4 5 7 8 9 10 Not working with: a=[1:10 1:10] > >> c=setdiff(a,b) > c = > 1 2 4 5 7 8 9 10 Better but it will reorder a, e.g., a = 10:-1:1 If keeping the order is important, then  a(ismember(a,b)) = [] % Bruno
 Subject: How to remove multiple values from array From: Steven_Lord Date: 7 May, 2012 13:26:12 Message: 6 of 12 "Bruno Luong" wrote in message news:jo7poa\$r5d\$1@newscl01ah.mathworks.com... > dpb wrote in message ... > >> >> a=1:10; >> >> a(intersect(a,b))=[] >> a = >> 1 2 4 5 7 8 9 10 > > Not working with: > > a=[1:10 1:10] > >> >> c=setdiff(a,b) >> c = >> 1 2 4 5 7 8 9 10 > > Better but it will reorder a, e.g., a = 10:-1:1 Not if you're using release R2012a or later and use the 'stable' setOrder flag. http://www.mathworks.com/help/techdoc/rn/bs8f84v-1.html#btah2bc >> a = 10:-1:1; >> b = [5 6]; >> c = setdiff(a, b, 'stable') c =     10 9 8 7 4 3 2 1 -- Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com
 Subject: How to remove multiple values from array From: Bruno Luong Date: 7 May, 2012 13:49:06 Message: 7 of 12 "Steven_Lord" wrote in message ... > > > "Bruno Luong" wrote in message > news:jo7poa\$r5d\$1@newscl01ah.mathworks.com... > > dpb wrote in message ... > > > >> >> a=1:10; > >> >> a(intersect(a,b))=[] > >> a = > >> 1 2 4 5 7 8 9 10 > > > > Not working with: > > > > a=[1:10 1:10] > > > >> >> c=setdiff(a,b) > >> c = > >> 1 2 4 5 7 8 9 10 > > > > Better but it will reorder a, e.g., a = 10:-1:1 > > Not if you're using release R2012a or later and use the 'stable' setOrder > flag. > > http://www.mathworks.com/help/techdoc/rn/bs8f84v-1.html#btah2bc > > >> a = 10:-1:1; > >> b = [5 6]; > >> c = setdiff(a, b, 'stable') > > c = > > 10 9 8 7 4 3 2 1 Good new feature. But in this specific question, setdiff sill fails on a=[10:-1:1 10:-1:1] because it only returns a unique elements of a, stable yes, but still miss the trailing part. % Bruno
 Subject: How to remove multiple values from array From: Peter Date: 7 May, 2012 14:38:07 Message: 8 of 12 Thanks for the ideas, I think I found a solution. First, my actual problem is a bit more complicated where a could be: a=[6 6 7 7 8 8 9 9 10 10 3 3 2 2 1 1 4 4 5 5]; let the undesired values be b=[7 3]; a(ismember(a,b)) returns a=[6 6 8 8 9 9 10 10 2 2 1 1 4 4 5 5]; keeping the order preserved and removing multiple entries. Cheers, Peter "Peter " wrote in message ... > For a given array of data I'd like to remove multiple values specified in another array. e.g. > > a=1:10 > b=[3 6] > =>remove b from a > result c=[ 1 2 4 5 7 8 9 10] > > I've looked at the any() command a(any(a=3))=[] will remove the 3 but I haven't found a way to remove an array of values without looping over the length of b. I know it all comes down to loops in the end, but for the size of data I thought Matlab might have an optimized routine for this - maybe one that would break up for multithreading instead of my one loop. > > Thanks for the ideas, > Peter
 Subject: How to remove multiple values from array From: Bruno Luong Date: 7 May, 2012 15:04:08 Message: 9 of 12 "Peter " wrote in message > > a(ismember(a,b)) > That's almost in the same line with the solution I posted earlier. a(ismember(a,b)) = [] Bruno
 Subject: How to remove multiple values from array From: Bruno Luong Date: 7 May, 2012 15:07:07 Message: 10 of 12 Oops I realize the type, it should be > > a(~ismember(a,b)) = [] > > Bruno
 Subject: How to remove multiple values from array From: Peter Date: 7 May, 2012 15:36:09 Message: 11 of 12 Why yes it is. Sorry about that, didn't check the email last night as I kept at it. I'm curious what you get for a(~ismember(a,b)) = [], I get Empty matrix: 1-by-0 a(logical(1-ismember(a,b)))=[] will return all the instances of values with b that are in a, i.e. [7 7 3 3] Peter "Bruno Luong" wrote in message ... > Oops I realize the type, it should be > > > > a(~ismember(a,b)) = [] > > > > Bruno
 Subject: How to remove multiple values from array From: Bruno Luong Date: 7 May, 2012 16:26:06 Message: 12 of 12 Oops I was actually right .The solution must be a = a(~ismember(a,b)) % or a(ismember(a,b)) = [] Your earlier post confuse me. Bruno

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### 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.