Keeping k largest values in each column of a sparse matrix

2 vues (au cours des 30 derniers jours)
Wolfgang Schwanghart
Wolfgang Schwanghart le 3 Juil 2011
Hi everyone,
I am trying to implement an algorithm that involves a pruning of a large sparse matrix. The pruning scheme should keep the k largest values (all nonzero values are positive) in each column of the sparse matrix M.
My current solution is:
function Mp = pruning(M,k)
[i,j,v] = find(M);
t = sortrows([i j v],[2 -3]);
dzero = cumsum([0;diff(t(:,2))]==0);
dz = [1;diff(t(:,2))]>0;
dd = dzero.*dz;
for r = 2:numel(dd);
dd(r) = max(dd([r-1 r]));
end;
I = (dzero-dd+1) <= k;
Mp = sparse(t(I,1),t(I,2),t(I,3),size(M,1),size(M,2));
Do you have better solutions (that avoid the loop)?
Thanks, W.
  1 commentaire
Wolfgang Schwanghart
Wolfgang Schwanghart le 19 Juil 2011
since nobody answered, I'll just give it another try...

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 19 Juil 2011
k = triu(bsxfun(@minus,dd,dd'));
pl = sum(k<0)+1==1:numel(dd);
m = dd(pl);
dd = m(cumsum(pl));
  8 commentaires
Sean de Wolski
Sean de Wolski le 19 Juil 2011
That is nice Andrei!
Andrei Bobrov
Andrei Bobrov le 19 Juil 2011
Thanks, friends!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by