zeros

Updated:


zeros can be used to avoid the dreaded NaN in model failure

Syntax

X = zeros(n,m,'like',sdpvar)

Examples

The typical scenario when we would use zeros is when we want to create a matrix which is almost all zeros, except for some elements.

The following version will fail as discussed in the post NaN in model

y = sdpvar(1,1);
X = zeros(5,5);
X(1,5) = y;

A natural way around this issue is to simply construct the matrix using concatenation

y = sdpvar(1,1);
X = [zeros(1,4) y;zeros(4,5)];

An alternative is to first create an overloaded zero, which is nothing but an sdpvar without any variables

y = sdpvar(1,1);
X = zeros(5,5,'like',sdpvar);
X(1,5) = y;

This only works in recent MATLAB versions though. An ugly alternative is double2sdpvar

y = sdpvar(1,1);
X = double2sdpvar(zeros(5,5))
X(1,5) = y;