K = speye(100,100); q1 = randn(100,1); u1 = K'*q1; population = [-1,1,0]; q2 = randsample(population,100, true)'; u2 = K'*q2; isSubgradient = @(u,K,q)((sum(abs(K*u))== sum((K'*q).*u)) & (max(abs(q))<=1)); if isSubgradient(u1,K,q1) disp(['The first example is a singular vector']); else disp(['The first example is not a singular vector']); end if isSubgradient(u2,K,q2) disp(['The second example is a singular vector']); else disp(['The second example is not a singular vector']); end