the code I'm posting may not be the most elegant and efficient way to solve you problem but seemingly it's working. Please let me know if you find any bug. Despite it is rare you will have repetitive elements with pseudo random number generator it is not impossible, especially if you are handling a huge number of elements (points). The code works also in case all rows of the initial guess are equal, hence with n-1 repetitions. I've assumed you are working with floating points in a predefined set, namely x in [a,b] and y in [c,d]. Please note that in case of repetitive elements the code perturbs the x-coord by a small deviation randomly generated. You may want to change this for example modifying the y-coordinate instead, or generating a new point and so on.
Dear Francesco Danzi Thank you so much for your reply
I think i didnt explain my self very well, i have one vector
V=[x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6]'. the idea is to check if the couple for exemple (x1 y1) isnt reapetable in the vector . and if so change one element in the couple. to do that, i wrote this lines which are working good
I am not so sure if there is a built in function in matlab which can find the index of the repeated value and replace it with a new one, because the vector in your code has a specific representation. Let say you have 5 main points, then you have 10 variables, 5 Xs and 5 Ys... one after one. The matlab can not recognize which one is X and which one is Y, so you need to use for loop just like what you did in the last comment.
-----------
I would suggest a totally different approach:
1- Use "Structure".
2- Use 'randperm(n,k) function, which helps you to generate k non repeated integer numbers between 1 to n.
I think the code should be like this, which is fast and clean:
x = randperm(10,5); % Generate 5 non-repeated integer random numbers
y = randperm(10,5); % In range [1,10]
Vector.X = x;
Vector.Y = y;
((OR))
x = randperm(10,5); % Generate 5 non-repeated integer random numbers
Thank you Sinan Salih for your valuable answer, actualy i have no control on the vector so i need to find away to get rid of the repeated couple without using the for loop to reduce the computational time
I will rephrase my question to be more clear . Lets assum that i have a vector as X=(1 3 5 7 1 3 8 9 5 7) note that i have no control on this vector . So what i need is to find away to change the repeated value in my case 5 7 and 1 3 without using for loop
Actually, my previous answer depends on the title of the post, because of (GA), any chromosom/ solution should be generated randomly .. so I said you can use randperm ...
Now, I have solved your problem without a single for loop, but using the same structure of the previous answer...
Algorithm:
1- Read Vector,
2- Split Vector into tow arrays X and Y, the odd positions (X) , even position (Y)
3- Get all values & indices in both arrays without repeating them .
4- Get the differences between the original and the unique indices for each array individually (Xd, Yd).
5- Get the differences between Xd and Yd, keep it in R.
6- if R is not empty, means that, there are some repeated values in X or Y (which is not problem), Goto 8
otherwise, there are some repeated values in the same index for X and Y, (which is the problem) Goto Step 7.
7. Swap the value of X into the value of Y, and viceversa
( to decrease the chance of generating the same or another repeated values)
8. Combine X and Y together and generate a new vector.
9. Display the results..
The code :
%Step 1 orignal vector
Vector = [ 1 3 5 7 1 3 8 9 5 7 ]
%Step 2 Convert to Struct
Points.X = Vector(1:2:end);
Points.Y = Vector(2:2:end);
% Step 3
[RX,Xind] = unique(Points.X);
[RY,Yind] = unique(Points.Y);
%Step 4
Xd = setdiff(1:length(Points.X),Xind);
Yd = setdiff(1:length(Points.Y),Yind);
%Step 5
R = setdiff(Xd,Yd);
%Step 6
if isempty(R)
%Step 7
Temp = Points.X(Xd);
Points.X(Xd) = Points.Y(Yd);
Points.Y(Yd) = Temp;
end
%Step 8
NewVector = zeros(1,length(Vector));
NewVector(1:2:end) = Points.X; % you can generate a new value for X
NewVector(2:2:end) = Points.Y; % you can generate a new value for Y
NewVector
The output:
Vector =
1 3 5 7 1 3 8 9 5 7
NewVector =
1 3 5 7 3 1 8 9 7 5
Notes
1) The values 3, 5 and 7 are repeated, but they are different points.
1,3
5,7
3,1
8,9
7,5
2) In step 8, I have generated a random integer number, but sometimes I got another repeated values, therefore, I have just swapped the values. You can re-generated a random number based on your equation, but you need to recheck it again ( In this case, a for loop is required ) .
3) The algorithm explained above, can solve the problem of two repeating values only ... for example if you have 1 , 3 , 1, 3 , 1, 3 the solution will be 1,3 , 3, 1 , 3 ,1 it produces 3,1 two times.. so if the original vectors contains some repeated points more than two times, then you can not use the swap ( Step 8), instead of that, regenerate a new random number.
4) May be you don't like my solution, however, I am interesting with this kind of problems .. so I want to say thank you because I tried and found a solution without using for loops..
I don't really know if that is the most efficient way, try "run&time" function, look at the time required for computation and see. Unique and setdiff functions, which I've also used in my previous answer, take time to compute. Moreover the reason I've used while loop is that when you generate another solution, you can accidentally create another repetitive solution so, to be sure there are no more repetitions, you need a further check. My impression is that there is a trade-off between the number of elements, the number of repetitions you have and the computational time and I'm afraid if you have few elements for is better than anything else. Indeed the code Bendine posted takes 0.001s to run while the other something around 0.02-0.03s (see images attached).
Dear Francesco Danzi thank you so much for the checking procedure you posted, it will defnitly help me to select the best option for my ptoblem . i will reply to all of you guys and provide the whole process iam involved in once i finish it