Index exceeds array elements
4 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Alex Mcaulley
on 26 Feb 2020
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 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!