Octave Programming Tutorial/Sets

SET

Octave set operation There are different set operations in octave basically Octave can use vector, cell arrays or matrix as its set input operation.

1) SET UNION:

The set union operations is one of the operations where two sets a and b are merged together.

In this example we will took two set namely ,A with contents 1,2,3 and B with contents 3,4,5. First we are going to use the set union operation using Octave. This can be accomplished using the union function available with octave.

 octave:1> a=[1,2,3]  
 a =  
   1  2  3  
 octave:2> b=[3,4,5]  
 b =  
   3  4  5  
 octave:3> union(a,b)  
 ans =  
   1  2  3  4  5

2) SET INTERSECTION :

The set intersection operations is one of the operations where two sets a and b are merged together and common element is taken.

In this example we will took two set namely A with contents 1,2,3 and b with contents 3,4,5 .First we are going to use the set intersection operation using Octave . This can be accomplished using the intersection function available with octave.

octave:1> a=[1,2,3]  
 a =  
   1  2  3  
 octave:2> b=[3,4,5]  
 b =  
   3  4  5  
 octave:3> intersect(a,b)  
 ans =  
   3

3) SET DIFFERENCE:

The set difference operations also called as the a-b operation is the operation which returns those element of a that are not in b. Lets write down the set difference operation as fallows :


Set=A-B

In this example we will took two set namely A with contents 1,2,3 and b with contents 3,4,5 First we are going to use the setdifference operation using Octave . This can be accomplished using the setdiff function available with octave. The difference operation is a Set operation in which those elements of a that are not in b are returned .

 octave:1> a=[1,2,3]  
 a =  
   1  2  3  
 octave:2> b=[3,4,5]  
 b =  
   3  4  5  
 octave:3> setdiff(a,b)  
 ans =  
   1,2

4) UNIQUE:

The unique operation returns a set containing one copy of each element in the given set, and with the elements in sorted order.

In this example we will take a set namely A with contents 1,2,4,3,5,2,1 containing the duplicate elements 1 and 2, and elements out of order. The unique operation will sort the set and remove extra copies of the duplicated elements.

 octave:1> a=[1,2,4,3,5,2,1]  
 a =
   1   2   4   3   5   2   1  
 octave:2> unique(a)
 ans =
   1   2   3   4   5
</syntaxhihglight>


4) XOR operation:
The Xor operation is one of the operation in which the two sets are compared  and if both the set evalulation remains the same then the result is return false else its return true . To execute set exor operation in octave we are going to use setxor function.
<syntaxhighlight lang="octave">
  a=[1,2,3]  
  a =  
    1  2  3  
  octave-3.2.4:34> b=[1,2,4]  
  b =  
    1  2  4  
  octave-3.2.4:35> setxor(a,b)  
  ans =  
    3  4

5) Ismember

The octave set ISMEMBER is one the function in which two sets are compared and the those elements that are present in the second set are marked as true rest are marked as false. This function is basically used to check which element are present in both the set.

 ocatave-3.2.4:33> a=[1,2,3]  
 a =  
   1  2  3  
  octave-3.2.4:34> b=[1,2,4]  
  b =  
    1  2  4  
  octave-3.2.4:35>  ismember(a,b)  
  ans =  
    [1 1 0]