Adding Penalty for Violation

2 次查看(过去 30 天)
I have this code snippet that generates a matrix with the notation of chr(:,:,i)
for i=1:10
A=zeros(100,4);
A(:,1)=randi(100,100,1); %Course(1)
A(:,2)=randi(5,100,1); %Day(2)
A(:,3)=randi(10,100,1); %Timeslot(3)
A(:,4)=randi(17,100,1); %Room(4)
chr(:,:,i)=A
end
Is there a way to check for duplicate values (Assuming Row 1, Row 4 & Row 5 contain similar data) and for each time a repeated data is found, a violation score (Let's say 10) is imposed to chr(:,:,i)
For the eg, if Row 4 & Row 5 are similar to Row 1, a violation score of 2*10 should be imposed to chr(:,:,i)

采纳的回答

Image Analyst
Image Analyst 2011-10-23
Well there are a variety of ways to do it, from brute force "for" loop ways to more efficient ways. After your for loop has finished creating the A array, I'd probably call sortrows() and then call diff(). Any row that has a repeat in it will have all zeros in a row of the array that diff() returns. Then check for all zero rows by using sum() or any(). Note which original row number this is by getting it from the index array that sortrows() returns (the second output arg). I expect that you're capable of handling the details, but that's how I'd start approaching it.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by