how do i assign an array to i?
4 views (last 30 days)
Show older comments
sharifah shuthairah syed abdullah
on 6 Jan 2019
Commented: madhan ravi
on 6 Jan 2019
i want to make an array in choosing PP.
RS=
1 28702 1 7 5 2 3 9 8 4 11 6 10 12
2 28764 9 2 7 6 3 1 4 5 12 10 11 8
3 36438 4 1 9 2 10 7 12 8 6 3 5 11
4 37192 12 7 10 8 1 9 4 6 5 11 3 2
5 41182 9 4 10 7 1 3 6 8 5 2 11 12
6 43442 4 10 2 3 1 5 6 12 9 8 11 7
7 48650 8 7 1 4 9 10 2 11 12 3 5 6
8 51154 12 2 8 1 3 5 7 4 11 9 6 10
9 51412 10 6 4 2 7 1 8 5 3 12 9 11
10 52796 8 2 4 5 3 6 1 7 12 11 9 10
11 54018 5 4 2 8 7 10 11 3 6 12 1 9
12 65420 6 7 2 8 3 4 12 1 11 9 10 5
cp=0.4; % Crossover Percentage
np=2*round(cp*r/2); % Number of Offsprings (Parents)
valp=np/2;
v=valp;
n1=length(RS(:,1));
PP=RS([1:v,n1-v+1:end],3:end);
% for cycle crossover used p1, p2,p3,p4 saja
p1 = PP(1,:);
p2 = PP(2,:);
p3 = PP(3,:);
p4 = PP(4,:);
instead of doing it manually, p1, p2, p3 and p4.. i want it to be an array so that whevever u change the value i didnt have to write it manually
however i got an error "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side" when i do it like
for i=1:np
p(i) = PP (i,:);
end
below is the result of PP
PP =
1. 12 3 9 11 6 10 4 7 5 1 8 2
2. 2 6 9 8 10 11 5 4 12 3 1 7
3. 10 4 2 12 3 8 9 5 1 6 11 7
4. 8 1 10 12 5 9 7 6 11 3 4 2
0 Comments
Accepted Answer
madhan ravi
on 6 Jan 2019
Edited: madhan ravi
on 6 Jan 2019
Don't think of naming variables dynamically (https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval) use cell instead:
P=cell(1,np); % preallocate
for i=1:np
P{i} = PP(i,:);
end
celldisp(P)
vertcat(P{:}) % double array
2 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!