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

Javier on 24 Feb 2025
Latest activity Reply by Javier on 19 Mar 2025 at 19:03

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.
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 at 19:03
Yeah, it works perfectly. But just like with my code, an error occurs in the "Test Suite"