Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find complementary values

Subject: find complementary values

From: Norman

Date: 20 Sep, 2010 15:16:04

Message: 1 of 10

This may be quite simple but I seem to have a brain block. arrgh.

I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.

Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.

Any help is welcome.

Cheers,
Norman

Subject: find complementary values

From: Matt J

Date: 20 Sep, 2010 15:28:05

Message: 2 of 10

"Norman " <nteferle@gmail.com> wrote in message <i77trk$2ih$1@fred.mathworks.com>...
> This may be quite simple but I seem to have a brain block. arrgh.
>
> I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.
>
> Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.

TheSet=1:100;
idx=ismember(TheSet,YourValues);
Misising = TheSet(~idx),

Subject: find complementary values

From: Wayne King

Date: 20 Sep, 2010 15:30:22

Message: 3 of 10

"Norman " <nteferle@gmail.com> wrote in message <i77trk$2ih$1@fred.mathworks.com>...
> This may be quite simple but I seem to have a brain block. arrgh.
>
> I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.
>
> Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.
>
> Any help is welcome.
>
> Cheers,
> Norman

Hi Norman,

help setdiff

B = randsample(100,100,true); % requires Statistics Toolbox
% samples 100 values with replacement from 1:100
C = setdiff(1:100,B);

Wayne

Subject: find complementary values

From: someone

Date: 20 Sep, 2010 15:40:05

Message: 4 of 10

"Wayne King" <wmkingty@gmail.com> wrote in message <i77ume$q87$1@fred.mathworks.com>...
> "Norman " <nteferle@gmail.com> wrote in message <i77trk$2ih$1@fred.mathworks.com>...
> > This may be quite simple but I seem to have a brain block. arrgh.
> >
> > I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.
> >
> > Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.
> >
> > Any help is welcome.
> >
> > Cheers,
> > Norman
>
> Hi Norman,
>
> help setdiff
>
> B = randsample(100,100,true); % requires Statistics Toolbox
> % samples 100 values with replacement from 1:100
> C = setdiff(1:100,B);
>
> Wayne

% I don't think setdiff works the way you think it does:


>> A = [0, 10, 11, 11, 15, 89];
>> B = 0:100;
>> c = setdiff(A, B)

c =

   Empty matrix: 1-by-0

Subject: find complementary values

From: Matt J

Date: 20 Sep, 2010 15:48:05

Message: 5 of 10

"someone" <someone@somewhere.net> wrote in message <i77v8l$546$1@fred.mathworks.com>...

> % I don't think setdiff works the way you think it does:
>
>
> >> A = [0, 10, 11, 11, 15, 89];
> >> B = 0:100;
> >> c = setdiff(A, B)
>
> c =
>
> Empty matrix: 1-by-0
============

It's because you have the input arguments interchanged.

Subject: find complementary values

From: Walter Roberson

Date: 20 Sep, 2010 15:53:43

Message: 6 of 10

On 20/09/10 10:40 AM, someone wrote:
> "Wayne King" <wmkingty@gmail.com> wrote in message

>> help setdiff
>>
>> B = randsample(100,100,true); % requires Statistics Toolbox
>> % samples 100 values with replacement from 1:100
>> C = setdiff(1:100,B);

> % I don't think setdiff works the way you think it does:

>>> A = [0, 10, 11, 11, 15, 89];
>>> B = 0:100;
>>> c = setdiff(A, B)
>
> c =
>
> Empty matrix: 1-by-0

You have reversed the order of the parameters relative to what Wayne
suggested. setdiff(A,B) finds the elements of A that are not in B. In
your example, there are no elements of A that are not also in B, so the
result is an empty matrix. In Wayne's example, the larger set is in the
A position, so the result would be the values _not_ sampled.

Subject: find complementary values

From: someone

Date: 20 Sep, 2010 17:25:10

Message: 7 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <bsLlo.99878$st2.46575@newsfe09.iad>...
> On 20/09/10 10:40 AM, someone wrote:
> > "Wayne King" <wmkingty@gmail.com> wrote in message
>
> >> help setdiff
> >>
> >> B = randsample(100,100,true); % requires Statistics Toolbox
> >> % samples 100 values with replacement from 1:100
> >> C = setdiff(1:100,B);
>
> > % I don't think setdiff works the way you think it does:
>
> >>> A = [0, 10, 11, 11, 15, 89];
> >>> B = 0:100;
> >>> c = setdiff(A, B)
> >
> > c =
> >
> > Empty matrix: 1-by-0
>
> You have reversed the order of the parameters relative to what Wayne
> suggested. setdiff(A,B) finds the elements of A that are not in B. In
> your example, there are no elements of A that are not also in B, so the
> result is an empty matrix. In Wayne's example, the larger set is in the
> A position, so the result would be the values _not_ sampled.

OOPS

That explains it!

Thanks

Subject: find complementary values

From: Roger Stafford

Date: 20 Sep, 2010 20:06:04

Message: 8 of 10

"Norman " <nteferle@gmail.com> wrote in message <i77trk$2ih$1@fred.mathworks.com>...
> This may be quite simple but I seem to have a brain block. arrgh.
>
> I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.
>
> Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.
>
> Any help is welcome.
>
> Cheers,
> Norman
- - - - - - - -
  To put the result in something like the missing-interval form you used, do the following. Let A be a row vector of your given numbers (chosen from among numbers 1 to 100).

 x = [0,sort(A),101];
 p = find(diff(x)>1);
 B = [x(p)+1;x(p+1)-1];

  Each of the elements in the top row of B give the initial number of a missing interval and the corresponding number in the bottom row gives the end of that interval. For example if A = [80 44 46 44], you would get

 B = [ 1 45 47 81
      43 45 79 100]

indicating that intervals 1-43, 45-45, 47-79, and 81-100 are missing in A.

Roger Stafford

Subject: find complementary values

From: Norman

Date: 22 Sep, 2010 12:22:04

Message: 9 of 10

Many thanks to Matt and everyone else who helped finding a solution to this.
Excellent!
Norman


"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i77ui5$hoe$1@fred.mathworks.com>...
> "Norman " <nteferle@gmail.com> wrote in message <i77trk$2ih$1@fred.mathworks.com>...
> > This may be quite simple but I seem to have a brain block. arrgh.
> >
> > I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.
> >
> > Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.
>
> TheSet=1:100;
> idx=ismember(TheSet,YourValues);
> Misising = TheSet(~idx),

Subject: find complementary values

From: Jos (10584)

Date: 22 Sep, 2010 12:43:05

Message: 10 of 10

"Norman " <nteferle@gmail.com> wrote in message <i77trk$2ih$1@fred.mathworks.com>...
> This may be quite simple but I seem to have a brain block. arrgh.
>
> I have a matrix of with numbers in the range between 1 and 100. The matrix does not contain all values between 1 and 100 and for some numbers there might be multiple instances. For example 0, 10, 11, 11, 15, ..., and 89.
>
> Is there a(n) (elegant) way to find those numbers between 1 and 100 that are not represented in the matrix? i.e., 1-9, 12-14, ...,90-100.
>
> Any help is welcome.
>
> Cheers,
> Norman

For continuous integers the following indexing trick is likely to be faster than setdiff

SomeNumbers = [2 4 6 4 3 9 6 2] % with repetitions
MyRange = 1:10
Result = MyRange % work on a copy
Result(SomeNumbers) = [] % remove from set

hth
Jos

Tags for this Thread

No tags are associated with this thread.

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.

Contact us