|
"Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i5ecr3$bof$1@fred.mathworks.com>...
> Hi,
>
> I've got the following line of code to remove columns containing only zeros:
> routes2=routes2(:,any(routes2));
>
> According to the profiler this line of code is good for 10% of my computational time. Is there any way to rewrite it or to use other statements so that this column removal process can be sped up?
>
> Thanks,
> Kurt
- - - - - - - - - -
As your code is now, every element of every column must be tested. If your matrix contains many nonzero columns and you have long columns, you might possibly speed things up with a for-loop using the find instruction. For each column do a find of that column using the second 'k' argument of 1 so that it (hopefully) stops after the first nonzero, and set the corresponding element of a logical vector according to whether the find result is non-empty or not. Then use that logical vector to eliminate the all-zero columns.
t = false(1,size(x,2));
for k = 1:size(x,2)
t(k) = ~isempty(find(x(:,k),1));
end
x = x(:,t);
Roger Stafford
|