|
From: | bpabbott |
Subject: | Re: Yet Another Vectorization Quiz |
Date: | Tue, 14 Dec 2010 22:39:10 +0000 (GMT) |
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.
[Prev in Thread] | Current Thread | [Next in Thread] |