Problem 859. Get the elements of diagonal and antidiagonal for any m-by-n matrix


I've been trying this problem a lot of time and i don't understand why my solution doesnt't work.
In 4 tests i get the error Assertion failed but when i run the code myself i get the diag and antidiag correctly.
function [diag_elements, antidg_elements] = your_fcn_name(x)
[m, n] = size(x);
% Inicializar los vectores de la diagonal y la anti-diagonal
diag_elements = zeros(1, min(m, n));
antidg_elements = zeros(1, min(m, n));
% Extraer los elementos de la diagonal
for i = 1:min(m, n)
diag_elements(i) = x(i, i);
end
% Extraer los elementos de la anti-diagonal
for i = 1:min(m, n)
antidg_elements(i) = x(m-i+1, i);
end
end
Christian Schröder
Christian Schröder on 24 Feb 2025
Very strange; this should indeed work. A problem with the Cody engine? @Arunika @Helen Chen
Chen Lin
Chen Lin on 25 Feb 2025
Hi Christian. @Vanessa Wang is our new lead developer for Cody. Loop her in.
Jose de Jesus
Jose de Jesus on 1 Feb 2026 at 0:47
@Chen Lin @Vanessa Wang Having the same problem as the others. Tried multiple ways but I'm still getting the assert error for all test cases.
Vanessa Wang
Vanessa Wang on 3 Feb 2026 at 18:05
Hi @Javier @Jose de Jesus, apologies for the delay in response. I took a closer look at the problem's test suite which includes this assertion:
% no diag
filetext = fileread('your_fcn_name.m');
assert(isempty(strfind(filetext, 'diag')))
However, this test was not appearing in the test suite since it started with % instead of %% which prevented Cody from correctly parsing this test and subsequently affected the other tests. I have updated the problem so that this assertion now shows up in the test suite. I understand this to be the problem author's way to prevent the diag function from being used but it appears to catch any string with diag. Renaming your variables to omit diag should allow the tests to pass.
In the future, please feel free to reach out to technical support if you do not hear back from us in a timely manner. This ensures that the issue will get reported to our team.
Christian Schröder
Christian Schröder on 25 Feb 2025
That's good to know, Chen, thank you!
Paul Bower
Paul Bower on 24 Feb 2025
Hi Javier,
Here's a heavily comment function which solves this problem. Hopefully, this code and its comments will help you solve the problem:
function [dg_elements, antidg_elements] = getDiagonalElements(inMatrix)
% getDiagonalElements extracts the main diagonal and anti-diagonal elements
% from any m-by-n matrix without using MATLAB's built-in diag() function.
%
% Input:
% inMatrix - an m-by-n matrix
%
% Outputs:
% dg_elements - a row vector containing the main diagonal elements
% antidg_elements- a row vector containing the anti-diagonal elements
% Get the number of rows (rr) and columns (cc) of the input matrix.
[rr, cc] = size(inMatrix);
% Create a mask matrix 'oo' that has ones along the diagonal of the
% square submatrix defined by the smaller dimension of inMatrix.
% This mask will be used for indexing the diagonal elements.
if rr >= cc
% Case 1: More rows than columns (or equal).
% Create an identity matrix of size cc, which has ones on the diagonal.
oo = eye(cc);
% Create a zero matrix to pad the identity matrix to have rr rows.
zz = zeros(rr-cc, cc);
% Concatenate the identity and zero matrices vertically to get an
% rr-by-cc mask. The ones remain in the positions of the diagonal.
oo = [oo; zz];
else
% Case 2: More columns than rows.
% Create an identity matrix of size rr, with ones on the diagonal.
oo = eye(rr);
% Create a zero matrix to pad the identity matrix to have cc columns.
zz = zeros(rr, cc-rr);
% Concatenate the identity and zero matrices horizontally to get an
% rr-by-cc mask. The ones remain in the positions of the diagonal.
oo = [oo, zz];
end % if
% Use the mask to extract the main diagonal elements.
% The expression (oo == 1) creates a logical index that is true for diagonal positions.
% The extracted elements are transposed to form a row vector.
dg_elements = inMatrix(oo == 1)';
% To extract the anti-diagonal elements (from top-right to bottom-left),
% first flip the input matrix vertically. This makes the anti-diagonal become the main diagonal.
for ii = 1:rr
% For each row, assign rows in reverse order to 'flipIn'.
flipIn(ii,:) = inMatrix(rr-ii+1,:);
end % for
% Now, apply the same mask 'oo' to the flipped matrix.
% This extracts the elements that were originally on the anti-diagonal.
antidg_elements = flipIn(oo == 1)';
end % function
Javier
Javier on 19 Mar 2025
Yeah, it works perfectly. But just like with my code, an error occurs in the "Test Suite"

Tags