help-octave
[Top][All Lists]
Advanced

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

Re: removing columns


From: Bård Skaflestad
Subject: Re: removing columns
Date: Tue, 3 May 2011 15:15:41 +0200

On Tue, 2011-05-03 at 14:53 +0200, Carnë Draug wrote:
> 2011/5/3 nuncio m <address@hidden>:
> > Hi Carne,
> >              My requirement is of the second kind.  HOwever 'sum' function
> > wont do the job as my columns contain negative and positive values.  I just
> > want to remove columns that are all zeroes.  I figured out another way using
> > max and min functions
> > matrix = 0  -1   2
> >              0  -1   2
> >              0  -1   2
> >              0   1   2
> >              0   2   3
> >
> >
> > c_to_r=find(max(mat1)==min(mat1))
> > matrix(:,c_t_or)=[]
> >
> > if 'sum' is used then 2nd column will also be removed
> >
> > Here also there is one problem if the all elements are same but other than
> > zero, then those columns will also be remove.  But at present I dont want
> > those columns either.
> 
> Then to remove all columns that have all zeros, taking into account
> that columns can have the same element on all rows, and negative and
> positive values who sum is zero, here's what you can do
> 
> index_of_equal = find(max(matrix) == min(matrix));
> columns_to_remove = index_of_equal(find(matrix(1,index_of_equal)==0));

Or better still: Extract all those columns whose values are *not* all
zero/equal:

    mat1 = mat1(:, ~all(bsxfun(@eq, mat1, mat1(1,:)));

This assumes you want exact equality checks which is OK for integral
data and a little more dubious for floating point.


Sincerely,
-- 
Bård Skaflestad <address@hidden>
SINTEF ICT, Applied Mathematics



reply via email to

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