function XAoptsimplex = proj_partition(X,m)

% This computes the closest partition matrix to X, by solving a linear
% programming problem using the simplex method

[n,k] = size(X);
en = ones(n,1);
ek = ones(k,1);

options = optimset('largescale','off','simplex','on');
% options = optimoptions(@linprog,'algorithm','simplex');
fsimplex = -X(:);
% fsimplex = fsimplex - 2*min(fsimplex)*ones(n*k,1);  % make obj. positive???

Asimplexeq = [kron(ek',eye(n));kron(eye(k),en')];   Asimplexeq = Asimplexeq(1:end-1,:);
bsimplexeq = [en;m];   bsimplexeq = bsimplexeq(1:end-1);

xaoptsimplex = linprog(fsimplex,[],[],Asimplexeq,bsimplexeq,zeros(n*k,1),[],[],options);
XAoptsimplex = reshape(xaoptsimplex,n,k);

% Truncate entries
inds = XAoptsimplex < .00001;
XAoptsimplex(inds) = 0;           % round small nos to 0
inds = XAoptsimplex > .99999;
XAoptsimplex(inds) = 1;            % round large nos to 1
%%% confirm feasibility
  if norm(XAoptsimplex*ones(k,1)-ones(n,1)) + norm(XAoptsimplex'*ones(n,1)-m) + norm(XAoptsimplex - logical(XAoptsimplex)) > .00001,
	    disp('error: XAoptsimplex not partition matrix?')
	    keyboard
  end