필터 지우기
필터 지우기

Why is my code giving an empty vector?

조회 수: 2 (최근 30일)
Kratos
Kratos 2015년 2월 21일
댓글: Image Analyst 2015년 2월 21일
The code that I have below works perfectly find when I do it in command window without using the while loop. I have to do it for a lot of time to get the answer but in the end I still get the answer. But when I run this function it gives me
out = [] and rupee = 0
why is it doing this? I have to stop when llllat =='D' and kkkkat == 0. I have the terminating command in the end but it looks like the loop doesn't end.
function [out rupee] = dat(rc, cdir, dtravel, letters, rvalues)
A = [];
B = [];
while A ~=0
row = rc(1);
erow = letters(row,:);
column = rc(2);
final = erow(column);
kat = rc(1);
kkat = dtravel(kat,:);
kkkat = rc(2);
kkkkat = kkat(column);
lat = rc(1);
llat = cdir(lat,:);
lllat = rc(2);
llllat = llat(column);
if llllat == 'D'
break;
end
pat = rc(1);
ppat = rvalues(pat,:);
pppat = rc(2);
ppppat = ppat(column);
for ind = 1:length(final)
A = [A final(ind)];
end
for ind = 1:length(ppppat)
B = [B ppppat(ind)];
end
if llllat == 'N'
w = rc(1);
b = w - kkkkat;
rc(1) = b;
elseif llllat == 'S'
w = rc(1);
b = w + kkkkat;
rc(1) = b;
elseif llllat == 'E'
w = rc(2);
b = w + kkkkat;
rc(2) = b;
else
w = rc(2);
b = w - kkkkat;
rc(2) = b;
if kkkkat == 0
break;
end
if llllat == 'D'
break;
end
end
end
out = A;
rupee = sum(B);
end
If you would like to check it I can post the test cases.
  댓글 수: 1
Stephen23
Stephen23 2015년 2월 21일
@Kratos: this code could be be much improved with some array preallocation and vectorizing most of the arithmetic. Because doing this makes for much neater and compact code (as well as being faster) you will be able to have a better overview of the code and understand its operation better.

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

답변 (1개)

Image Analyst
Image Analyst 2015년 2월 21일
I don't know what your input parameters are so about all I can say is that usually when you want to compare strings you use strcmp(), strcmpi(), or strfind(), not the == operator. Try that and also try using the debugger to step through your code one line at a time to figure out why the loop never exits.
  댓글 수: 2
Kratos
Kratos 2015년 2월 21일
I did that at terminating condition but it didn't work. When you input
rc = [2,4];
cdir =['ESWENSWW;
SNSESNEW;
NSEENEND;
NEEWWNEW']
letters = ['e cdefgr;
ijeManoe;
qSdlur!d;
twabsoef']
rvalues = [5,1,1,10,1,5,20,1;5,5,1,100,1,1,5,1;50,1,5,5,1,20,10,1;1,5,5,20,5,1,1,1]
dtravel = [7,2,18,42,12,1,2,6;3,4,1,1,2,20,19,5;30,1,4,25,15,2,1,0;3,4,10,9,4,1,8,7]
You will get the answer 'Master Sword' and 142 if you do it on the command window couple of times without using the while loop but as soon as i run it with while loop I get an empty vector.
Image Analyst
Image Analyst 2015년 2월 21일
When you used the debugger, did the code execution follow different paths? Or did you not even use the debugger at all?

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by