Deleting equal rows/columns in matrix

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Deleting equal rows/columns in matrix

AlbFrigerio
Hello world, I got a problem I'm trying to solve on my own, but I really don't know if I would solve it, hence I'm asking "in advance" your help.

The question is quite simple : is there a (simple) way to delete in a matrix the already existing rows or columns?

I might use for loops asking isequal(A(i,:),A(j,:) \forall i,j , but I hope there is a nicer way. I tried to use the outer product with function @isequal , but I reach results I don't understand :(
Just as an information, I don't have a real matrix, but an integer one whose lines are combination of {0,1,9} . If I got only {0,1} values I would have used binary transformation, but I got three elements and, by the way, I'm looking for the solution of the general problem.

Thank you so much, in the rest of the day I'll try to solve it, if I don't reach any solution ... I hope someone of yours do !!

   Alberto
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

bpabbott
Administrator
On Oct 20, 2010, at 9:27 PM, AlbFrigerio <[hidden email]> wrote:

>
> Hello world, I got a problem I'm trying to solve on my own, but I really
> don't know if I would solve it, hence I'm asking "in advance" your help.
>
> The question is quite simple : is there a (simple) way to delete in a matrix
> the already existing rows or columns?

For a matrix, A, the fourth row is deleted by ...

A(4,:) = [];

Ben
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

Fotios Kasolis
In reply to this post by AlbFrigerio

On Oct 20, 2010, at 3:27 PM, AlbFrigerio wrote:

>
> Hello world, I got a problem I'm trying to solve on my own, but I  
> really
> don't know if I would solve it, hence I'm asking "in advance" your  
> help.
>
> The question is quite simple : is there a (simple) way to delete in  
> a matrix
> the already existing rows or columns?
>
> I might use for loops asking isequal(A(i,:),A(j,:) \forall i,j , but  
> I hope
> there is a nicer way. I tried to use the outer product with function
> @isequal , but I reach results I don't understand :(
> Just as an information, I don't have a real matrix, but an integer  
> one whose
> lines are combination of {0,1,9} . If I got only {0,1} values I  
> would have
> used binary transformation, but I got three elements and, by the  
> way, I'm
> looking for the solution of the general problem.
>
> Thank you so much, in the rest of the day I'll try to solve it, if I  
> don't
> reach any solution ... I hope someone of yours do !!
>
>   Alberto
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Deleting-equal-rows-columns-in-matrix-tp3003845p3003845.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

a) If you want to remove entire rows/cols use an assignment to the  
empty matrix like
 > A = repmat (1:5,5,1)
 > A(:,1) = [] # this line removes the first column

b) Just for your information if you want to conditionally modify  
elements use the function find
 >  A = repmat (1:5,5,1)
 > A(find (A>3)) = 1

the same could be done (without using find) as follows
 >  A = repmat (1:5,5,1)
 > A(A>3) = 1

To relate the above to your problem you can set for instance a special  
value to the elements you want to remove
 >  A = repmat (1:5,5,1)
 > A(A>3) = nan

and then extract a matrix without those nans by using

 > B = A(! isnan(A))

Enjoy!

/Fotios
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

AlbFrigerio
Thank you Ben and Fotios, you gave me correct answers, but I believe I have not explained well my problem. I don't want to delete "una tantum" a row or a column, I want that my program, given the following matrix as input

A = [1 0 1 0 9 0 1 ; 1 0 1 0 1 9 1 ; 0 1 9 1 0 1 9 ;  1 1 1 1 1 0 9 ; 1 0 1 0 1 9 1; 1 1 1 1 0 1 9]

deletes the 5th row because it is the same of the 2nd one, or the 4th column (equal to the 2nd one). Obviosly it is "easy" to look manually for the duplicates in a small matrix like the previous one, but imagine your matrix to have more rows and colums.

Thanks again,
  Alberto


Fotios Kasolis wrote
On Oct 20, 2010, at 3:27 PM, AlbFrigerio wrote:

>
> Hello world, I got a problem I'm trying to solve on my own, but I  
> really
> don't know if I would solve it, hence I'm asking "in advance" your  
> help.
>
> The question is quite simple : is there a (simple) way to delete in  
> a matrix
> the already existing rows or columns?
>
> I might use for loops asking isequal(A(i,:),A(j,:) \forall i,j , but  
> I hope
> there is a nicer way. I tried to use the outer product with function
> @isequal , but I reach results I don't understand :(
> Just as an information, I don't have a real matrix, but an integer  
> one whose
> lines are combination of {0,1,9} . If I got only {0,1} values I  
> would have
> used binary transformation, but I got three elements and, by the  
> way, I'm
> looking for the solution of the general problem.
>
> Thank you so much, in the rest of the day I'll try to solve it, if I  
> don't
> reach any solution ... I hope someone of yours do !!
>
>   Alberto
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Deleting-equal-rows-columns-in-matrix-tp3003845p3003845.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> Help-octave@octave.org
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

a) If you want to remove entire rows/cols use an assignment to the  
empty matrix like
 > A = repmat (1:5,5,1)
 > A(:,1) = [] # this line removes the first column

b) Just for your information if you want to conditionally modify  
elements use the function find
 >  A = repmat (1:5,5,1)
 > A(find (A>3)) = 1

the same could be done (without using find) as follows
 >  A = repmat (1:5,5,1)
 > A(A>3) = 1

To relate the above to your problem you can set for instance a special  
value to the elements you want to remove
 >  A = repmat (1:5,5,1)
 > A(A>3) = nan

and then extract a matrix without those nans by using

 > B = A(! isnan(A))

Enjoy!

/Fotios
_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

bpabbott
Administrator

On Oct 20, 2010, at 10:25 PM, AlbFrigerio wrote:

>
> Fotios Kasolis wrote:
>>
>> On Oct 20, 2010, at 3:27 PM, AlbFrigerio wrote:
>>
>>> Hello world, I got a problem I'm trying to solve on my own, but I  
>>> really
>>> don't know if I would solve it, hence I'm asking "in advance" your  
>>> help.
>>>
>>> The question is quite simple : is there a (simple) way to delete in  
>>> a matrix
>>> the already existing rows or columns?
>>>
>>> I might use for loops asking isequal(A(i,:),A(j,:) \forall i,j , but  
>>> I hope
>>> there is a nicer way. I tried to use the outer product with function
>>> @isequal , but I reach results I don't understand :(
>>> Just as an information, I don't have a real matrix, but an integer  
>>> one whose
>>> lines are combination of {0,1,9} . If I got only {0,1} values I  
>>> would have
>>> used binary transformation, but I got three elements and, by the  
>>> way, I'm
>>> looking for the solution of the general problem.
>>>
>>> Thank you so much, in the rest of the day I'll try to solve it, if I  
>>> don't
>>> reach any solution ... I hope someone of yours do !!
>>>
>>>  Alberto
>>> --
>>
>> a) If you want to remove entire rows/cols use an assignment to the  
>> empty matrix like
>>> A = repmat (1:5,5,1)
>>> A(:,1) = [] # this line removes the first column
>>
>> b) Just for your information if you want to conditionally modify  
>> elements use the function find
>>> A = repmat (1:5,5,1)
>>> A(find (A>3)) = 1
>>
>> the same could be done (without using find) as follows
>>> A = repmat (1:5,5,1)
>>> A(A>3) = 1
>>
>> To relate the above to your problem you can set for instance a special  
>> value to the elements you want to remove
>>> A = repmat (1:5,5,1)
>>> A(A>3) = nan
>>
>> and then extract a matrix without those nans by using
>>
>>> B = A(! isnan(A))
>>
>> Enjoy!
>>
>> /Fotios
>
> Thank you Ben and Fotios, you gave me correct answers, but I believe I have
> not explained well my problem. I don't want to delete "una tantum" a row or
> a column, I want that my program, given the following matrix as input
>
> A = [1 0 1 0 9 0 1 ; 1 0 1 0 1 9 1 ; 0 1 9 1 0 1 9 ;  1 1 1 1 1 0 9 ; 1 0 1
> 0 1 9 1; 1 1 1 1 0 1 9]
>
> deletes the 5th row because it is the same of the 2nd one, or the 4th column
> (equal to the 2nd one). Obviosly it is "easy" to look manually for the
> duplicates in a small matrix like the previous one, but imagine your matrix
> to have more rows and colums.
>
> Thanks again,
>  Alberto

Take a look at unique().

        help unique

To get the unique rows of a matrix, X, ...

        X = unique (X, "rows")

To get the columns ...

        X = unique (X.', "rows").'

Does that do what you want?

Ben


_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

AlbFrigerio
In reply to this post by AlbFrigerio
I forgot a thing : in the case of the previous matrix I could transform every row in an integer number my making

A * 10.^[size(A,2)-1:-1:0]' ,

in this case if in two rows I get the same value it means that the same rows in the matrix are equal. Similar procedure using A' for the columns?

Three problems for the general case :
1) What should I do if a matrix entry is greater than 9 ?
2) What should I do if the matrix has real entries ?
3) What should I do if the matrix has both positive and negative entries ?

   Alberto


AlbFrigerio wrote
Thank you Ben and Fotios, you gave me correct answers, but I believe I have not explained well my problem. I don't want to delete "una tantum" a row or a column, I want that my program, given the following matrix as input

A = [1 0 1 0 9 0 1 ; 1 0 1 0 1 9 1 ; 0 1 9 1 0 1 9 ;  1 1 1 1 1 0 9 ; 1 0 1 0 1 9 1; 1 1 1 1 0 1 9]

deletes the 5th row because it is the same of the 2nd one, or the 4th column (equal to the 2nd one). Obviosly it is "easy" to look manually for the duplicates in a small matrix like the previous one, but imagine your matrix to have more rows and colums.

Thanks again,
  Alberto


Fotios Kasolis wrote
On Oct 20, 2010, at 3:27 PM, AlbFrigerio wrote:

>
> Hello world, I got a problem I'm trying to solve on my own, but I  
> really
> don't know if I would solve it, hence I'm asking "in advance" your  
> help.
>
> The question is quite simple : is there a (simple) way to delete in  
> a matrix
> the already existing rows or columns?
>
> I might use for loops asking isequal(A(i,:),A(j,:) \forall i,j , but  
> I hope
> there is a nicer way. I tried to use the outer product with function
> @isequal , but I reach results I don't understand :(
> Just as an information, I don't have a real matrix, but an integer  
> one whose
> lines are combination of {0,1,9} . If I got only {0,1} values I  
> would have
> used binary transformation, but I got three elements and, by the  
> way, I'm
> looking for the solution of the general problem.
>
> Thank you so much, in the rest of the day I'll try to solve it, if I  
> don't
> reach any solution ... I hope someone of yours do !!
>
>   Alberto
> --
> View this message in context: http://octave.1599824.n4.nabble.com/Deleting-equal-rows-columns-in-matrix-tp3003845p3003845.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> Help-octave@octave.org
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

a) If you want to remove entire rows/cols use an assignment to the  
empty matrix like
 > A = repmat (1:5,5,1)
 > A(:,1) = [] # this line removes the first column

b) Just for your information if you want to conditionally modify  
elements use the function find
 >  A = repmat (1:5,5,1)
 > A(find (A>3)) = 1

the same could be done (without using find) as follows
 >  A = repmat (1:5,5,1)
 > A(A>3) = 1

To relate the above to your problem you can set for instance a special  
value to the elements you want to remove
 >  A = repmat (1:5,5,1)
 > A(A>3) = nan

and then extract a matrix without those nans by using

 > B = A(! isnan(A))

Enjoy!

/Fotios
_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

AlbFrigerio
In reply to this post by bpabbott
YES!!!!!

Thank you so much , I hadn't found it in the manual ... how stupid I am!! By the way, everybody says that the best way to learn programming is to program yourself : I made my own program and, with entries between 0 and 9, it is faster than Octave ones :)

Thanks again,
   Alberto

bpabbott wrote
On Oct 20, 2010, at 10:25 PM, AlbFrigerio wrote:
>
> Fotios Kasolis wrote:
>>
>> On Oct 20, 2010, at 3:27 PM, AlbFrigerio wrote:
>>
>>> Hello world, I got a problem I'm trying to solve on my own, but I  
>>> really
>>> don't know if I would solve it, hence I'm asking "in advance" your  
>>> help.
>>>
>>> The question is quite simple : is there a (simple) way to delete in  
>>> a matrix
>>> the already existing rows or columns?
>>>
>>> I might use for loops asking isequal(A(i,:),A(j,:) \forall i,j , but  
>>> I hope
>>> there is a nicer way. I tried to use the outer product with function
>>> @isequal , but I reach results I don't understand :(
>>> Just as an information, I don't have a real matrix, but an integer  
>>> one whose
>>> lines are combination of {0,1,9} . If I got only {0,1} values I  
>>> would have
>>> used binary transformation, but I got three elements and, by the  
>>> way, I'm
>>> looking for the solution of the general problem.
>>>
>>> Thank you so much, in the rest of the day I'll try to solve it, if I  
>>> don't
>>> reach any solution ... I hope someone of yours do !!
>>>
>>>  Alberto
>>> --
>>
>> a) If you want to remove entire rows/cols use an assignment to the  
>> empty matrix like
>>> A = repmat (1:5,5,1)
>>> A(:,1) = [] # this line removes the first column
>>
>> b) Just for your information if you want to conditionally modify  
>> elements use the function find
>>> A = repmat (1:5,5,1)
>>> A(find (A>3)) = 1
>>
>> the same could be done (without using find) as follows
>>> A = repmat (1:5,5,1)
>>> A(A>3) = 1
>>
>> To relate the above to your problem you can set for instance a special  
>> value to the elements you want to remove
>>> A = repmat (1:5,5,1)
>>> A(A>3) = nan
>>
>> and then extract a matrix without those nans by using
>>
>>> B = A(! isnan(A))
>>
>> Enjoy!
>>
>> /Fotios
>
> Thank you Ben and Fotios, you gave me correct answers, but I believe I have
> not explained well my problem. I don't want to delete "una tantum" a row or
> a column, I want that my program, given the following matrix as input
>
> A = [1 0 1 0 9 0 1 ; 1 0 1 0 1 9 1 ; 0 1 9 1 0 1 9 ;  1 1 1 1 1 0 9 ; 1 0 1
> 0 1 9 1; 1 1 1 1 0 1 9]
>
> deletes the 5th row because it is the same of the 2nd one, or the 4th column
> (equal to the 2nd one). Obviosly it is "easy" to look manually for the
> duplicates in a small matrix like the previous one, but imagine your matrix
> to have more rows and colums.
>
> Thanks again,
>  Alberto

Take a look at unique().

        help unique

To get the unique rows of a matrix, X, ...

        X = unique (X, "rows")

To get the columns ...

        X = unique (X.', "rows").'

Does that do what you want?

Ben


_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

Jaroslav Hajek-2
On Wed, Oct 20, 2010 at 5:33 PM, AlbFrigerio
<[hidden email]> wrote:
>
> YES!!!!!
>
> Thank you so much , I hadn't found it in the manual ... how stupid I am!! By
> the way, everybody says that the best way to learn programming is to program
> yourself : I made my own program and, with entries between 0 and 9, it is
> faster than Octave ones :)
>

That is certainly interesting. Can you post a code demonstrating this?
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

AlbFrigerio
I believe Octave function is better than mine for lots of reasons, mainlty because my function works only with matrix of integer 0:9 values with small size. I tried it on some matrices, and the time discrepance is not big. I implemented it only because I wasn't able to find the unique function, now I'm using it because it is more flexible (also with big size matrices).

My idea is quite simple : given a matrix, I transform every row in a number using 10-base representation. Then I produce the outer difference of the resulting vector, looking for the non-diagonal zeros. These are the repeated elements, I choose to delete the latest ones.

You may find the attached codes. Since I cannot understand how to use the outer function to make outer difference (I tried outer(B,B,@diff) or outer(B,-B,@sum) but they didn't work) I created the outermio function.

By the way, just an information on unique function. Is it possible to know how much rows were equal to the lines which have not been deleted?  For example, if I got a 6 row matrix with equal rows 1 - 2 - 6 and 3 - 5 , my resultant matrix will const of previous rows 1 - 3 - 4 , and I'd like to have a vector b whose elements are [3 2 1] , i.e. b(i) = the number of rows in the input matrix which are equal to the i-th row of the unique one.

Have all a nice day,
   Alberto

outermio.m
xcs_remove_duplicate.m
AA.txt

Jaroslav Hajek-2 wrote
On Wed, Oct 20, 2010 at 5:33 PM, AlbFrigerio
<alberto.frigerio1@studenti.unimi.it> wrote:
>
> YES!!!!!
>
> Thank you so much , I hadn't found it in the manual ... how stupid I am!! By
> the way, everybody says that the best way to learn programming is to program
> yourself : I made my own program and, with entries between 0 and 9, it is
> faster than Octave ones :)
>

That is certainly interesting. Can you post a code demonstrating this?
_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

Jaroslav Hajek-2
On Thu, Oct 21, 2010 at 12:30 PM, AlbFrigerio
<[hidden email]> wrote:

>
> I believe Octave function is better than mine for lots of reasons, mainlty
> because my function works only with matrix of integer 0:9 values with small
> size. I tried it on some matrices, and the time discrepance is not big. I
> implemented it only because I wasn't able to find the unique function, now
> I'm using it because it is more flexible (also with big size matrices).
>
> My idea is quite simple : given a matrix, I transform every row in a number
> using 10-base representation. Then I produce the outer difference of the
> resulting vector, looking for the non-diagonal zeros. These are the repeated
> elements, I choose to delete the latest ones.
>

Yes, I understand all that. I was just curious whether you can show me
a case where this approach actually beats Octave's unique by a
significant margin. I'm still interested, but please give the *exact*
code including the time measurements, I can't guess what you actually
did.
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Deleting equal rows/columns in matrix

AlbFrigerio
OK Jaroslav, here it is the code I implemented. The matrix A was my input, as you may find in the attached file. I tried to do the same thing with general matrices of the same size (see the initial commented lines) but every time unique Octave beated mine ... probably I've been lucky in my example!! By the way, my function doesn't sort the final matrix as uniques does.

% NR = 500;
% NC = 10;
% A = round(9*rand(NR,NC));
% for i=1:NR
        % if rand<0.5
                % r = 1+(NR-1)*round(rand);
                % A(i,:)=A(r,:);
        % endif
% endfor

A=load("A.txt").A;

t=time;
A_u = unique(A,"rows");
t1=time-t

A_u2 = A;
t=time;
B=A_u2*10.^[size(A_u2,2)-1:-1:0]';
D = repmat(B,1,length(B)) - repmat(B',length(B),1);
[a,b]=find(D==0);
pos=find((a-b)!=0);
       
if ~isempty(pos)
        pos=[a,b](pos,:);
        pos = pos(find(pos(:,1)>pos(:,2)),1);
        val = pos;
        if length(pos)>1
                val = values(pos);
        endif
        pos=val;
endif
A_u2(pos,:)=[];
t2=time-t

t1<t2

<nabble_a href="A.txt">A.txt

Jaroslav Hajek-2 wrote
Yes, I understand all that. I was just curious whether you can show me
a case where this approach actually beats Octave's unique by a
significant margin. I'm still interested, but please give the *exact*
code including the time measurements, I can't guess what you actually
did.
_______________________________________________
Help-octave mailing list
Help-octave@octave.org
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave