```Path: news.mathworks.com!not-for-mail
From: "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com>
Newsgroups: comp.soft-sys.matlab
Subject: FAST algorithm to jenga matrix?
Date: Sun, 29 Mar 2009 02:17:01 +0000 (UTC)
Organization: Stanford University
Lines: 48
Message-ID: <gqmlmt\$6tp\$1@fred.mathworks.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1238293021 7097 172.30.248.38 (29 Mar 2009 02:17:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 29 Mar 2009 02:17:01 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:528410

I have a GIANT sparse matrix with scattered non-trivial elements ranging from 1:N, then I need to remove a M non-trivial elements and maintain the cardinality from 1:N-M. Is there a fast way to do it? Here's an example to illustrate what I'm trying to do (N=8, M=2):

>> A=[ 0 0 8 0 0 ; 1 4 0 0 2; 0 5 3 0 0; 7 0 0 6 0]
A =
0     0     8     0     0
1     4     0     0     2
0     5     3     0     0
7     0     0     6     0

>> sort(nonzeros(A))
ans =
1
2
3
4
5
6
7
8

------------------------------
Numbers to delete:  2, 6

desiredMatrix =
0     0     6(-2) 0     0
1     3(-1) 0     0     X
0     4(-1) 2(-1) 0     0
5(-2) 0     0     X     0

desiredMatrix =
0     0     6     0     0
1     3     0     0     0
0     4     2     0     0
5     0     0     0     0

>> sort(nonzeros(desiredMatrix))
ans =
1
2
3
4
5
6

Because the matrix is huge (but sparse), I don't want to use for loops. I kind of have a feeling that there must be a slick way to do it with a different way of thinking, and there must be some MATLAB master-minds here who can come up with a slick one-or-two-liners. :)