필터 지우기
필터 지우기

what is wrong?

조회 수: 2 (최근 30일)
Sahab
Sahab 2011년 5월 7일
it should show answer of -30,why it show -10?what is wrong?
x=[33,11,33,11,22,33,11];
r4penalty=0;
r3penalty=0;
r2penalty=0;
r1penalty=0;
penalty=0;
for ii = 1:size(x,1)
c = 1;
Y = x(ii,:)==33;
while c <= length(x)-1
if Y(c)
if x(ii,c+1)==11
r4penalty=-10;
c= c + 1;
elseif Y(c+1:c+2) % Three in a row. (33,33,33)
r3penalty=-30;
c = c + 4;
elseif x(ii,c+1)==33 % Two in a row.(33,33,11)
r2penalty=-20;
c = c + 3;
elseif x(ii,c+1)==11 % Just one 33. (33,11)
r1penalty=-10;
c = c + 2;
elseif x(ii,c+1)==22 % pattern 33,22
c = c + 1;
end
else
c = c + 1;
end
end
penalty(ii,1)=r1penalty+r2penalty+r3penalty+r4penalty
end
did somebody know?

채택된 답변

Amir Hamzah UTeM
Amir Hamzah UTeM 2011년 5월 7일
try do this,
total=0;
penalty=0;
for ii = 1:size(x,1)
c = 1;
Y = x(ii,:)==33;
while c <= length(x)-1
if Y(c)
if x(ii,c+1)==11
penalty1=-10;
total(ii,1)=total+penalty1
c= c + 1;
elseif Y(c+1:c+2) % Three in a row. (33,33,33)
penalty2=-30;
total(ii,1)=total+penalty2
c = c + 4;
elseif x(ii,c+1)==33 % Two in a row.(33,33,11)
penalty3=-20;
total(ii,1)=total+penalty3
c = c + 3;
elseif x(ii,c+1)==11 % Just one 33. (33,11)
penalty4=-10;
total(ii,1)=total+penalty4
c = c + 2;
elseif x(ii,c+1)==22 % pattern 33,22
c = c + 1;
end
else
c = c + 1;
end
end
total(ii,1)=total
end

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 5월 7일
The vectorized solution to your problem:
x = [33,11,33,11,22,33,11];
% Find the 33s that are immediately before 22
bon = nnz(strcmp(x,[33 22]));
% Count the total number of 33
mal = nnz(x == 33);
% Total penalty = -(total33 - 33before22) * single penalty
penalty = -(mal-bon)*10;
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2011년 5월 7일
or
t = x==33;
penalty= -(sum(t) - sum(x(find(t)+1)==22))*10;

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by