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


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


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) = x;

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) = x;

Leave a Comment