📄 nnz.m
字号:
function varargout = nnz(varargin)
%NNZ (overloaded)
%
% n = nnz(X)
%
% The NNZ operator is implemented using the concept of nonlinear operators
% in YALMIP. NNZ(X) creates a new so called derived variable that can be
% treated as any other variable in YALMIP. When SOLVESDP is issued,
% logic constraints are added to the problem to model the NNZ operator.
% Author Johan L鰂berg
% $Id: nnz.m,v 1.6 2005/05/09 14:21:34 joloef Exp $
switch class(varargin{1})
case 'char'
z = varargin{2};
x = varargin{3};
[n,m] = size(x);
F = set([]);
totalcount = 0;
if issymmetric(x)
delta = binvar(n,n); % delta==1 means x(i,j)==0
for i = 1:n
for j = i:n
xij=ihatethis(x,i,j);
dij=ihatethis(delta,i,j);
if isa(xij,'sdpvar')
F = F + set(implies(dij,xij==0));
if i==j
totalcount = totalcount + (1-dij);
else
totalcount = totalcount + 2*(1-dij);
end
else
if xij~=0
if i==j
totalcount = totalcount + 1;
else
totalcount = totalcount + 2;
end
end
end
end
end
else
delta = binvar(n,m);
for i = 1:n
for j = 1:m
xij=ihatethis(delta,i,j);
dij=ihatethis(delta,i,j);
if isa(xij,'sdpvar')
F = F + set(iff(dij,xij==0));
totalcount = totalcount + (1-dij);
else
if xij~=0
totalcount = totalcount + 1;
end
end
end
end
end
depends(delta)
F = F + set(z == totalcount);% + set(integer(z));
varargout{1} = F;
varargout{2} = 0;
varargout{3} = [];
case 'sdpvar'
varargout{1} = yalmip('addextendedvariable','nnz',varargin{:});
otherwise
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -