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 ?

 채택된 답변

VBBV
VBBV 2022년 11월 26일
popnew1 = 2-A(mutated);

댓글 수: 10

VBBV
VBBV 2022년 11월 26일
편집: VBBV 2022년 11월 27일
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 2022년 11월 27일
편집: VBBV 2022년 11월 27일
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 .

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2022년 11월 26일

댓글:

2022년 11월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by