From: us on
"Ross Anderson" <rpa5nospam(a)cornell.edu> wrote in message <i180dd$sid$1(a)fred.mathworks.com>...
> Hi all,
>
> I have Ax=B where A and B are sparse. I want to reduce this so any row ax=b is unique, but I don't have the resources to make A and B full first. If I could, I could say
>
> Afull = [full(A) full(B)];
> [newmat,index] = unique(Afull,'rows','first');
> repeatedIndex = setdiff(1:size(Afull,1),index);
> Af = full(A);
> Bf = full(B);
> Af(repeatedIndex,:) = [];
> Bf(repeatedIndex)=[];
> A = sparse(Af);
> B = sparse(Bf);
>
> but is there a way to do this without un-sparsifying A and B?
> I have the components of A and B in vectors
> A = sparse(rows(:,1),rows(:,2),rows(:,3),numrows,sizeX);
> and I can guarantee that for a row the non-zero elements are in ascending order. I was thinking I might search the matrix rows for a block [rows(:,2) rows(:,3)] with a different rows(:,1). But that seems overly complicated and slow.
>
> Suggestions?

one of the other solutions is outlined below
- note: run this in a function(!)...

function atest
% the data
n=1000000;
a=zeros(n,3);
b=zeros(n,3);
a([1,3,9],:)=repmat(10*(1:3),3,1);
b([3,4,8],:)=repmat(10*(1:3),3,1);
a(10,:)=ceil(100*rand(1,3));
b([2,5],:)=ceil(-100*rand(2,3));
as=sparse(a);
bs=sparse(b);
% the engine
tic;
[ax,ay,av]=find([as,bs]);
ss=unique([ax,ay,av],'rows','first');
r=sparse(ss(:,1),ss(:,2),ss(:,3));
r=unique(full(r),'rows','first');
t1=toc;
tic;
da=unique([as,bs],'rows','first'); % <- ori data
t2=toc;
% the result
disp(isequal(r,full(da)));
disp([{'t1';'t2';'gain'},num2cell([t1;t2;t2/t1])]);
%{
% wintel: ic2/2*2.6ghz/2gb/winxp.sp3.32/r2010a.32
1 % <- equal results
't1' [0.0011105]
't2' [ 0.43159]
'gain' [ 388.63]
% ru =
0 0 0 -45 -77 -66
0 0 0 -45 -66 -35
0 0 0 0 0 0
0 0 0 10 20 30
10 20 30 0 0 0
10 20 30 10 20 30
24 12 61 0 0 0
%}
end

us