Linear matrix transformation that keeps the first nonzero element in each row of a matrix and zeros the rest

51 Views Asked by At

I need to find a matrix transformation that takes a (non-square) matrix and within each of its rows keeps the first nonzero element in that row and zeros out the rest of the entries within that row.

I tried to solve the linear matrix equation AX = B to get the matrix transformation A as A = B X^T (XX^T)^{-1} , where X is the matrix to be transformed as such, and B is the desired output matrix (X with only its first nonzero element in each row) but I look for a more straightforward and possibly more elegant way to do it. I appreciate creative answers!

Thanks!