How to make the condition for checking the matrix value ?

function [popnew1,mutated] =mutation(A,Pm)
mutated = find(rand(size(A))<Pm);
popnew1 = A;
% Why my this condition is not working
if A(mutated)== 1
popnew1(mutated) = 2-A(mutated);
else
popnew1(mutated) = 1-A(mutated);
end
%
end
Please Help me why my this condition part is not working Where A is 3x8 matrix and Pm=0.1 .
Where is the problem in my condition ?

 Akzeptierte Antwort

VBBV
VBBV am 26 Nov. 2022
popnew1 = 2-A(mutated);

10 Kommentare

VBBV
VBBV am 26 Nov. 2022
Bearbeitet: VBBV am 27 Nov. 2022
You need to assign popnew1 as above, after substituting the indices in A matrix to get new popnew1.
Can you tell me which steps i need to change in my coding ? Because i did not understand your advise .
If you can show me in my coding .
Thanks in advance .
Or can you set any condition where if the mutated point value is zero then only it will change to 1 ,Otherwise if the mutated point is 1 then it will not be changed ,means it will be same as 1
VBBV
VBBV am 27 Nov. 2022
Bearbeitet: VBBV am 27 Nov. 2022
Why do you want substitute the mutated indices in popnew1 instead of assigning it ?. Look at the change suggested for the line in beginning. mutated is indices vector where the values of A are less than Pm.
if A(mutated)== 1
popnew1 = 2-A(mutated);
else
popnew1 = 1-A(mutated);
end
Sorry to say this logic is not working ,its only showing the mutated point
May be the below change you need to do
A = eye(3,8) % The A matrix
A = 3×8
1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
Pm = 0.1;
[popnew1,mutated] = mutation(A,Pm)
popnew1 = 3×8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
mutated = 21×1
2 3 4 6 7 8 10 11 12 13
function [popnew1,mutated] = mutation(A,Pm)
mutated = find(A<Pm);
popnew1 = A;
% Why my this condition is not working
if A(mutated) == 1
popnew1(mutated) = 2-popnew1(mutated); % in place of A put popnew1
else
popnew1(mutated) = 1-popnew1(mutated); % in place of A put popnew1
end
%
end
if A(mutated) == 1
popnew1(mutated) = 2-popnew1(mutated); % in place of A put popnew1
else
popnew1(mutated) = 1-popnew1(mutated); % in place of A put popnew1
end
Thank you for your suggestion .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Gefragt:

am 26 Nov. 2022

Kommentiert:

am 28 Nov. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by