Index exceeds array elements

5 次查看(过去 30 天)
Johnny Thai
Johnny Thai 2020-2-26
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 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by