turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-27-2014 11:02 PM

Hi,

I have two vectors like

A = {6,4,3,2,5,1};

B = {2,3};

I want to get another vector which contains the different elements comparing A and B. So I tried using SETDIF function.

C = setdif(A,B);

which returns the vector C is {1 4 5 6}. But, what I want is the not-sorted vector {6 4 5 1}.

Does anyone know how I could get the vector I want?

Thanks very much!

Accepted Solutions

Solution

02-28-2014
09:47 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Amy_W

02-28-2014 09:47 AM

I am thinking there should be a simpler solution than this:

C = remove( A, loc( ( (repeat(A, 1, nrow(B) )=t(B)) [ ,+] ) > 0) );

But I can't see any. This works for your example, but note that it will retain any repeated values in A, so A={6,4,3,2,5,1,4} would return {6 4 5 1 4}.

All Replies

Solution

02-28-2014
09:47 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Amy_W

02-28-2014 09:47 AM

I am thinking there should be a simpler solution than this:

C = remove( A, loc( ( (repeat(A, 1, nrow(B) )=t(B)) [ ,+] ) > 0) );

But I can't see any. This works for your example, but note that it will retain any repeated values in A, so A={6,4,3,2,5,1,4} would return {6 4 5 1 4}.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanWakeling

02-28-2014 12:50 PM

Thank you, IanWakeling. I have searched for hours yesterday before I posted the question here and I didn't find anything simpler than your solution here. Actually I would like to retain repeated values in the vector C so thanks again.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Amy_W

02-28-2014 03:46 PM

Use the ELEMENT function:

A = {6,4,3,2,5,1};

B = {2,3};

C = A[ loc(^element(A,B)) ];

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

02-28-2014 05:19 PM

That's perfection. Thank you Rick.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

03-04-2014 12:27 PM

Thank you, Rick. That is awesome.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

03-19-2014 10:28 AM

I blogged about this problem: Finding elements in one vector that are not in another vector - The DO Loop

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

04-14-2014 12:30 PM

I saw it. I subscribed your blog. Very helpful.