% function U = pargrp(M) % Input: % M = mask, pixels of the same label except the minimum one % are constrained to group together % Output: % U = partial grouping constraints, such that U'X = 0 % specify the partial grouping info % % Stella X. Yu, July 12 2003. function U = pargrp(M) if isempty(M), U = []; return; end m = min(M(:)); % the minimum label is ignored n = max(M(:)); i = []; j = []; v = []; nu = 0; for k=m+1:n, t = find(M==k); s = length(t); if s<2, continue; end i = [i; t(1:s-1); t(2:s)]; z = nu+[1:s-1]'; j = [j; z; z]; nu = z(end); z = ones(s-1,1); v = [v; z; -z]; end U = sparse(i,j,v,prod(size(M)),nu);