필터 지우기
필터 지우기

how do i assign an array to i?

조회 수: 2 (최근 30일)
sharifah shuthairah syed abdullah
댓글: madhan ravi 2019년 1월 6일
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

채택된 답변

madhan ravi
madhan ravi 2019년 1월 6일
편집: madhan ravi 2019년 1월 6일
P=cell(1,np); % preallocate
for i=1:np
P{i} = PP(i,:);
end
celldisp(P)
vertcat(P{:}) % double array
  댓글 수: 2
sharifah shuthairah syed abdullah
thank you so much.. i didnt know about cell... thank you for telling me
madhan ravi
madhan ravi 2019년 1월 6일
Anytime :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by