2.56 How to remove values from one vector that exist in another vector

2.56.1 Mathematica
2.56.2 Matlab
2.56.3 Fortran
2.56.4 Maple

Given vector \(v1=\{1,3,4,5,8,9,20.2,30,-44\}\) remove from it any element that exist in \(v2=\{1,7,4\}\). Notice that Complement[] in Mathematica or setdiff() in Matlab can be used, but they will sort the result. Hence they are not used here in order to keep the order the same.

2.56.1 Mathematica

#first method 
v1={1,3,4,5,8,9,20.2,30,-44}; 
v2={1,7,4}; 
v1=DeleteCases[v1,Alternatives@@v2]
 

{3,5,8,9,20.2,30,-44}

 

#second method 
v1={1,3,4,5,8,9,20.2,30,-44}; 
v2={1,7,4}; 
v1=DeleteCases[v1,x_/;MemberQ[v2,x]]
 

{3,5,8,9,20.2,30,-44}

 

2.56.2 Matlab

v1=[1,3,4,5,8,9,20.2,30,-44]; 
v2=[1,7,4]; 
v1(ismember(v1,v2))=[]
 

v1 = 
3.0000 5.0000 8.0000 9.0000 20.2000 30.0000 -44.0000
 

 

2.56.3 Fortran

program f  implicit none 
  REAL, ALLOCATABLE :: A(:),B(:) 
  A =[1.0,3.0,4.0,5.0,8.0,9.0,20.2,30.0,-44.0]; 
  B =[1.0,7.0,4.0]; 
  A = pack(A, A .NE. B) 
  Print *,A 
end program f
 
>gfortran f2.f90 
>./a.out 
3.0000000 5.0000000 8.0000000  9.0000000  20.200001  30.000000 -44.000000
 

2.56.4 Maple

v1:=Array([1,3,4,5,8,9,20.2,30,-44]); 
v2:=Array([1,7,4]); 
select[flatten](x->not(member(x,v2)),v1)
 

[3, 5, 8, 9, 20.2, 30, -44]