help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Yet Another Vectorization Quiz


From: Carlo de Falco
Subject: Yet Another Vectorization Quiz
Date: Tue, 14 Dec 2010 23:12:12 +0100

Hi all,

I need to shift all the zeros in a (possibly very large) matrix 
to the end of each column without changing the ordering of the 
remaing elements, e.g.:

a = [ 1 2 4 0         [ 1 2 4 6
      2 3 5 6    ==>    2 3 5 7
      3 0 0 7           3 4 6 0
      0 4 6 0]          0 0 0 0]

with a for loop this can be done as

for j = 1:columns (a)
  v = zeros (rows (a), 1);
  v(1:nnz (a(:, j))) = a(a(:, j) != 0, j);
  a(:, j) = v;
endfor

I personally see no obvious way to improve this, 
does anyone have any idea?

Thanks,
c.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]