Error that does not make sense.

I am calling a function which is meant to give an error if n is greater than 10; however, the values I am assigning to "n" are less than 10, but the error is still coming up and I do not understand why.
ndarray = [6, 8, 9;
4, 8, 6] ;
%top row is possible n values, bottom row is possible d values.
[numberRounds1] = crack_code(ndarray(1,1),ndarray(2,1)); %first set of n and d values
[numberRounds2] = crack_code(ndarray(1,2),ndarray(2,2)); %second set of n and d values
[numberRounds3] = crack_code(ndarray(1,3),ndarray(2,3)); %third set of n and d values
Here is the function these values are needed for:
[numberRounds] = crack_code(n,d)
Within this function I am calling the function that is giving the error.
Here is the part of the function that is giving the eroor.
% Sanity checks
if n>10
error("Alphabet size (n) canot exceed 10");
end
if d>n
error("Number of digits (d) cannot exceed alphabet size (n)");
end

7 comentarios

Torsten
Torsten el 11 de Mzo. de 2022
You didn't tell us which error you encountered.
Did you assign a value to "numberRounds" in "crack_code" ?
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
I encountered an error from the function here:
if n>10
error("Alphabet size (n) canot exceed 10");
end
if d>n
error("Number of digits (d) cannot exceed alphabet size (n)");
end
My teacher provided this function but it has given me multiple errors so far.
Yes, I did assign a value to "numberRounds" in "crack_code"
Jan
Jan el 11 de Mzo. de 2022
You show us some code, which contain 2 different error() commands and tell us, that you get an error. Torsten has mentioned already, that you do not tell us, which of the two errors you get. Then posting the code again does not clarify the question.
Torsten
Torsten el 11 de Mzo. de 2022
Editada: Torsten el 11 de Mzo. de 2022
Is
[numberRounds] = crack_code(n,d)
the call to crack_code or the function definition ?
If it's the function definition, the line
[numberRounds] = crack_code(n,d)
must read
function [numberRounds] = crack_code(n,d)
Did you take this into account ?
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
Editada: Walter Roberson el 11 de Mzo. de 2022
Yes, "[numberRounds] = crack_code(n,d)" is just to use the function.
This is the function that it is calling:
function [numberRounds] = crack_code(n,d)
guess_array = gen_array(n,d); %calls the gen_array function and creates all the posibilities of the code.
[rows,~]=size(guess_array);
code = datasample(gen_array,1,1) ;
guess = datasample(gen_array,1,1);
TF = true (rows, 1) ;
numberRounds = 1;
numberMatches = 0;
while (numberMatches < d) && (numberRounds <= 8)
[nmatches] = check_matches(guess,code) ;
for i = (1:rows)
[matches] = check_matches(guess,guess_array(i,:)) ;
if matches ~= nmatches
TF(i) = false ;
end
end
guess_array = guess_array(TF,:);
guess = datasample(guess_array,1,1);
[rows,~]=size(guess_array);
TF = true (rows, 1) ;
numberRounds = numberRounds + 1;
end
%% Subfunction
function [nmatches] = check_matches(array1,array2)
nmatches = sum(array1 == array2);
end
end
Torsten
Torsten el 11 de Mzo. de 2022
You still don't tell us what the error message from MATLAB is.
Is it a secret ?
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
I am telling you that the error is from a line in a function which tells matlab to give an error when n>10.
if n>10
error("Alphabet size (n) canot exceed 10");
Hence my confusion when my values of n are less than 10 but this error still occurs.

Iniciar sesión para comentar.

Respuestas (3)

Voss
Voss el 11 de Mzo. de 2022
Is it possible there is another crack_code.m that supercedes this one on the path so that that other one is being called instead of this one? You can do the following to find out:
which crack_code -all
Or is it possible that ndarray changes before it is used in crack_code?
As it is, this function seems to work ok:
ndarray = [6, 8, 9, 11 7;
4, 8, 6, 10 9];
%top row is possible n values, bottom row is possible d values.
for jj = 1:size(ndarray,2)
try
[numberRounds] = crack_code(ndarray(1,jj),ndarray(2,jj));
disp(numberRounds);
catch ME
disp(ME.message);
end
end
0 0 0
Alphabet size (n) canot exceed 10 Number of digits (d) cannot exceed alphabet size (n)
function [numberRounds] = crack_code(n,d)
% Sanity checks
if n>10
error("Alphabet size (n) canot exceed 10");
end
if d>n
error("Number of digits (d) cannot exceed alphabet size (n)");
end
numberRounds = 0;
end

8 comentarios

Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
The error is coming from a function my proffesor gave the class, not an error from something within my functions I think.
Voss
Voss el 11 de Mzo. de 2022
What does
which crack_code -all
show, when you run it on the command line?
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
C:\Users\lukew\Downloads\Luke_Witherow_hw5.m (crack_code) % Local function of Luke_Witherow_hw5
This is what it shows, crack_code is a local function.
Voss
Voss el 11 de Mzo. de 2022
OK. Looks like the problem is probably not because of multiple functions called crack_code on the path.
Can you attach the file Luke_Witherow_hw5.m?
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
I attached my code and the function from my proffesor.
Thank you!
Voss
Voss el 11 de Mzo. de 2022
When I run it, I get an error in gen_array (not enough input arguments) caused by this:
code = datasample(gen_array,1,1) ;
guess = datasample(gen_array,1,1);
because those lines are calling gen_array with no arguments. I believe they should be:
code = datasample(guess_array,1,1) ;
guess = datasample(guess_array,1,1);
i.e., using the result returned from the previous call to gen_array(), which is guess_array.
When I change those two lines, it seems to run ok.
Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
Thank You this seems to have worked!
Voss
Voss el 11 de Mzo. de 2022
You're welcome!
If you don't mind, please mark the answer as 'Accepted' so that others know the problem seems to have been resolved.

Iniciar sesión para comentar.

Jan
Jan el 11 de Mzo. de 2022
You can be completely sure, that the error occurs only, if the conditions are met. Matlab is not tired or evil, but a deterministic system. Use the debugger to find out, what's going on: Set a breakpoint in the two lines containing the error() commands. Or let Matlab stop at all errors automatically:
dbstop if error
Now let Matlab run and when it stops, check the condition manually.
You did not mention, if the n>10 or d>n condtionen caused the error. But if you check the vcalues of d and n, this will be clear immediately.
A possible source of the error is, that you have two versions of the function and run not the one you are expecting. Check this by:
which crack_code -all
and the same for the main script or function.

1 comentario

Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
This did not help, my values of n are 6, 8, and 9, none of which are greater than 10, but the code still gives an error at the line meant to give an error when n>10

Iniciar sesión para comentar.

Steven Lord
Steven Lord el 11 de Mzo. de 2022
At first I thought you might be running into the common non-scalar if condition problem. From the documentation of the if keyword: "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." [Bold emphasis added.]
So in the statement:
% Block commenting so I can run code later in this Answer
%{
if n > 10
%}
that condition is satisfied only if ALL the elements of n are greater than 10. If you have a million elements in n that are greater than 10 and one that is not greater than 10, the if condition is not satisfied.
If you want that condition to be satisfied if ANY of the values in the expression are true, use the any function.
n = 1:10;
if n > 5 % not satisfied
disp("All elements of n were greater than 5")
else
disp("NOT all elements of n were greater than 5")
end
NOT all elements of n were greater than 5
if any(n > 5) % Satisfied
disp("At least one element of n was greater than 5")
else
disp("NONE of the elements of n were greater than 5")
end
At least one element of n was greater than 5
But reading more carefully, I wonder if the data you're showing us is the data on which you're operating. Are you passing a number into your function or the text representation of that number?
number = 1
number = 1
textRepresentation = '1'
textRepresentation = '1'
The variable named number contains a value less than 10. The variable named textRepresentation contains a character whose character code is greater than 10.
isNumberTooLarge = number > 10 % false
isNumberTooLarge = logical
0
isTextTooLarge = textRepresentation > 10 % true
isTextTooLarge = logical
1
double(textRepresentation)
ans = 49

1 comentario

Luke Witherow
Luke Witherow el 11 de Mzo. de 2022
so are you thinking that this is an issue within the gen_array function?

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 11 de Mzo. de 2022

Comentada:

el 11 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by