Index exceeds array elements

조회 수: 6 (최근 30일)
Johnny Thai
Johnny Thai 2020년 2월 26일
댓글: Alex Mcaulley 2020년 2월 27일
I'm taking a class for matlab, so I apologize for messy or improper formatting. I've set to write a code for one of the assignments, but in my for loop, the loop index goes above the length of the vector even though that shouldn't be the case. I'm wondering why this is an issue. There was a given function gen_vector, but I do not see that as the issue.
function CodeBreaker = hw5__AA_func(n,d)
%%Given Guess Vector
guess_vector = gen_vector(6,4);
%% The Code that the "bot" must find out
code = guess_vector(randi(length(guess_vector)));
%%The first guess in the vector
guess = guess_vector(1);
%% True/False Vector used to remove incorrect guesses
TF = true(length(guess_vector));
%%Number of rounds
rounds = 1;
%%Number of Digits that match code
match = check_matches(guess,code);
while (guess ~= code & rounds <= 8)
for (j = 1:length(guess_vector))
%%Checking digits in every instance of the vector
N_Match1 = check_matches(guess,guess_vector(j))
%%if an index does not match the given match, then the
%%corresponding value in TF is 0.
if N_Match1 ~= match
TF(j) = 0;
else
TF(j) = 1;
end
if TF(j) == 0
%%If an index in TF is 0, then the corresponding index in the
%%guess vector is removed, then the length of the new vector is
%%reestablished as is the true/false vector
guess_vector(j) = []
guess_vector
else
end
end
end
%%New Guess
guess = guess_vector(1);
%%Increase number of rounds
rounds = rounds + 1;
%% True/false vector length equal to the new length
TF = true(length(guess_vector));
end
function N_match = check_matches(n1,n2)
dig1 = dec2base(n1,10)-'0';
dig2 = dec2base(n2,10)-'0';
N_match = length(find(dig1 == dig2));
end
  댓글 수: 2
KSSV
KSSV 2020년 2월 26일
What input did you try?
Johnny Thai
Johnny Thai 2020년 2월 26일
I didn't try any particular input, I just tried running the function and I got that error for j. Technically (6,4) is the used input.

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

답변 (1개)

Alex Mcaulley
Alex Mcaulley 2020년 2월 26일
The problem is in this line (you are changing the size of the array inside the loop):
guess_vector(j) = []
One option could be:
function CodeBreaker = hw5__AA_func(n,d)
%%Given Guess Vector
guess_vector = gen_vector(6,4);
%% The Code that the "bot" must find out
code = guess_vector(randi(length(guess_vector)));
%%The first guess in the vector
guess = guess_vector(1);
%% True/False Vector used to remove incorrect guesses
TF = true(length(guess_vector));
%%Number of rounds
rounds = 1;
%%Number of Digits that match code
match = check_matches(guess,code);
while (guess ~= code & rounds <= 8)
toRemove = false(size(guess_vector)); %Change
for (j = 1:length(guess_vector))
%%Checking digits in every instance of the vector
N_Match1 = check_matches(guess,guess_vector(j))
%%if an index does not match the given match, then the
%%corresponding value in TF is 0.
if N_Match1 ~= match
TF(j) = 0;
else
TF(j) = 1;
end
if TF(j) == 0
%%If an index in TF is 0, then the corresponding index in the
%%guess vector is removed, then the length of the new vector is
%%reestablished as is the true/false vector
toRemove(j) = true; %Change
end
end
guess_vector(toRemove) = []; %Change
%%New Guess
guess = guess_vector(1);
%%Increase number of rounds
rounds = rounds + 1;
%% True/false vector length equal to the new length
TF = true(length(guess_vector));
end
function N_match = check_matches(n1,n2)
dig1 = dec2base(n1,10)-'0';
dig2 = dec2base(n2,10)-'0';
N_match = length(find(dig1 == dig2));
end
  댓글 수: 2
Johnny Thai
Johnny Thai 2020년 2월 26일
While that has removed the initial problem, the proposed solution creates the error "matrix index is out of range for deletion"
Alex Mcaulley
Alex Mcaulley 2020년 2월 27일
What value of guess_vector are you using?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by