# How can I minimize the number of non-zero elements in the solution vector subject to linear constraints (MATLAB)?

all, I've tried to find an answer to this question and have come across some related threads but nothing I was able to apply to my problem given my relative lack of experience in this arena.

I basically have a constrained minimization problem using both inequality and equality constraints. I will have more variables (~10) than equality constraints (6), so generally speaking there will be an infinite number of solutions to the system. The wrinkle is that I want my solution vector to have as many zero elements as possible, or more precisely I want to minimize the number of non-zero elements. The real-world application is that each variable represents a trade in a portfolio, so I want to hit my "targets" in the portfolio with the fewest number of necessary trades.

My initial approach was to use the fmincon function in Matlab with the objective function as

fun=@(x)nnz(x);


But this approach results in a spurious solution even for the most basic test example I could come up with. Is a problem like this solvable in Matlab? Any help would be much appreciated, I'm really at an impasse. Thanks.

A common solution to this problem when you have a cardinality constraint (or an $\ell_0$ regularization term, it's not clear to me from your description), is to relax the non-convex cardinality constraint to either a convex $\ell_1$ constraint, or a convex $\ell_1$ regularization term.
So, for example, a problem like $$\text{minimize}\quad \mathbf{card}(x), \qquad \text{subject to}\quad x\in\mathcal{C}$$ could be (heuristically) replaced with $$\text{minimize}\quad \|x\|_1, \qquad \text{subject to}\quad x\in\mathcal{C}.$$ A constraint like $\mathbf{card}(x)\leq k$ could be replaced by a regularization term $\gamma\|x\|_1$, or a constraint $\|x_1\|\leq \beta$, where the appropriate values of $\beta$ or $\gamma$ have to be determined later to make the result match the original constraint.