function [A , rmme] = rmn( A , pn , alredrmvd )
%RMN  randomly remove nodes from graph
%
%   RMN(A,pn,rm)  randomly removes nodes from the network (given by 
%      its adjacency matrix A) by filling the corresp. rows and cols
%      with NaN entries.  The no. of deleted nodes depends on
%      - if 0 < pn < 1  the probability that a node is removed
%      - if pn >= 1  the actual number of nodes to be removed
%
%  Last change:  10/2/2005 - Florian Knorn

%  The vector  alredrmvd  contains nodes that have already been
%  removed. (This may be used in conjunction with other func-
%  tions that need to keep track of the removed nodes.)

if nargin < 2 || nargin > 3
	error('Please provide at least A and pn !');
else
	n = size(A,1);
	if pn < 0 || (pn >= 1 && round(pn) ~= pn)
		error('pn must be a probab. (0 <= pn < 1) or an int >= 1 !');
	end
	if nargin <= 2
		mustfindnans = true;
	else
		mustfindnans = false;
	end
end

if pn < 1 % we're dealing with probabilities
	pn = binornd(lnind,pn);
end

if pn > n
	error('Cannot remove more nodes then actually exist !');
end

if mustfindnans
	alredrmvd = find(findnans(A));
end

rp = randperm(n);			% gen. rand. perm
rmme = rp(1:pn);			% keep pn of those

if rm % completely remove nodes (and edges)
	A(rmme,:) = [];
	A(:,rmme) = [];

	% report original node names that just got removed...
	origind = 1:n+length(alredrmvd); % get [1 2 3 ... orig_n]
	origind(alredrmvd) = [];% get surivors names
	rmme = origind(rmme);	% report removed ones within
else % replace corresp. rows and cols with NaNs
	A = double(A);
	A(rmme,:) = NaN;
	A(:,rmme) = NaN;
end