How to make the condition for checking the matrix value ?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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 ?
Accepted Answer
VBBV
on 26 Nov 2022
popnew1 = 2-A(mutated);
10 Comments
You need to assign popnew1 as above, after substituting the indices in A matrix to get new popnew1.
Akash Pal
on 27 Nov 2022
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 .
Akash Pal
on 27 Nov 2022
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
Akash Pal
on 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.
VBBV
on 27 Nov 2022
if A(mutated)== 1
popnew1 = 2-A(mutated);
else
popnew1 = 1-A(mutated);
end
Akash Pal
on 27 Nov 2022
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
VBBV
on 27 Nov 2022
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
Akash Pal
on 28 Nov 2022
Thank you for your suggestion .
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)