Problem 859. Get the elements of diagonal and antidiagonal for any m-by-n matrix
Javier
il 24 Feb 2025 alle 12:34
Ultime attività Replicato da Christian Schröder
il 25 Feb 2025 alle 17:23
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
4 Commenti
Accedi per partecipare
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