- create A using random numbers. This way, the actual values are different every time the code executes, meaning learners have to index A in order to get the correct answer. You can leave the semicolon off so the matrix values display in the output, showing that they change every time. To do this, place the following code in both the reference and learner solutions: A = randi(10,3,3)
- approach 2 is to make A and subMat so large that hardcoding the answer is extremely painful. This won't matter for assessing - computers are great at comparing thousands of numbers. However, to make them sufficiently large, you would likely be generating them using built-in functions like randi, so this may not be necessary.
MATLAB Code Solution Assessment
51 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kuda
el 23 de Dic. de 2024 a las 11:22
Editada: Cris LaPierre
el 23 de Dic. de 2024 a las 16:18
I am trying to assess a question below
Given
A = [1 2 3; 4 5 6; 7 8 9]
derive a 2x2 matrix at the bottom right corner of A and store it in variable subMat
I want students to use appropriate MATLAB syntax below
subMat = A(2:3,2:3)
But it seems I can only assess whether the answer is correct but not the syntax. So a student could type
subMat = [5 6; 8 9]
and the assessment will mark it as correct.
How do I assess the syntax?
0 comentarios
Respuesta aceptada
Cris LaPierre
el 23 de Dic. de 2024 a las 13:16
Editada: Cris LaPierre
el 23 de Dic. de 2024 a las 16:18
There is no built-in way to check how learners created subMat. However, there are 2 aproaches you could take that would prevent learners from hard coding a solution.
Note that both the reference and learner solutions contain the same seed so will generate the same random numbers. See this Answer for more.
0 comentarios
Más respuestas (2)
Jayanti
el 23 de Dic. de 2024 a las 12:04
Editada: Jayanti
el 23 de Dic. de 2024 a las 12:06
Hi Kuda,
You can assess if the answers contain a correct syntax by using regular expressions to match the expected pattern.
Please refer to the below function which checks if a pattern is present in the code for generalised n*n matrix.
function checkSyntax(code, n)
pattern = sprintf('A\\(%d:%d,%d:%d\\)', n-1, n, n-1, n);
if ~isempty(regexp(code, pattern, 'once'))
disp('Correct syntax used.');
else
disp('Incorrect syntax.');
end
end
MATLAB's “regexp” function will search for this pattern within the code.
% Here's how you can use it:
n = 3;
code = 'subMat = A(2:3,2:3)';
checkSyntax(code, n);
Hope this will be helpful!
1 comentario
Rik
el 23 de Dic. de 2024 a las 14:54
This gets complicated fast if you want to allow all legal syntax options:
% Here's how you can use it:
n = 3;
code = 'subMat = A(2:3,2:3)';
checkSyntax(code, n);
code = 'subMat = A( 2 : 3 ,2:3)';
checkSyntax(code, n);
function checkSyntax(code, n)
pattern = sprintf('A\\(%d:%d,%d:%d\\)', n-1, n, n-1, n);
if ~isempty(regexp(code, pattern, 'once'))
disp('Correct syntax used.');
else
disp('Incorrect syntax.');
end
end
In this case you can splatter in some \s* (or \S*, I always need to check the doc which case is the space), but for harder problems this get very unwieldy for robust checking. Even minify will not really help much.
Image Analyst
el 23 de Dic. de 2024 a las 16:06
I recommend that you do not do that (check for exact syntax of their solution). Why not? Because your answer is not the only, or even the best, way to answer that question. In fact, a more robust and general way to solve it would be
A = [1 2 3; 4 5 6; 7 8 9]
subMat = A(end-1:end, end-1:end)
rather than yours, and this could be marked incorrect. If you want you could examine the code for specific ways to hard code the final answer, as you showed, but I don't think you should be examining the source code for a match to your code because there are alternative ways to arrive at the answer, as I have shown. (In fact the Cody feature of this website is based upon that.)
0 comentarios
Ver también
Categorías
Más información sobre Script-Based Unit Tests en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!