How do I remove columns or rows that are identical?

13 views (last 30 days)
At first, I underestimated this by thinking that there was an easy solution and there may be but I wasn't able to find it due to my inexperience as a MATLAB user. After spending hours looking for a solution, trying various things such as taking advantage of the find command or using set operation to even using unique, it was all to no avail. I would greatly appreciate the help.
Lets say I have two randomly generated vectors with equal size or length, suppose the elements a through f are numbers that are in the reals:
x = [a b c c]; % Note: The Unique command would not help here, because a and b may also be equal to c.
y = [e f d d]; % Note: These two are always equal in size, but aren't constrained to only having 4 elements.
% These vectors aren't necessarily randomly generated, but to me, they might as well be because I have no idea what they have unless I go in to look.
The goal is to make a 2 x n matrix out of those two:
h = [x;y]; % In this case, it would result in a 2x4 matrix which two columns that are identical to each other.
In essense:
% Make it go from:
h = [a b c c; e f d d];
% To
h = [a b; e f]; % Note: The resulting matrix will always be a 2xn matrix. In other words, it is not constrained to become 2x2.
This modified matrix would then be compared to another matrix that has unique columns with the isequal command.
I will be working on this for some time, because this is a crucial step to what I was working on, so if I come across a solution, I will post it here.
Thank you,
Farruh.
Edit 1: Please assume that the identical columns are not adjacent, such as
h = [a b c b; b d f d];
Edit 2: An alternative would be to remove columns with identical indices, such as
h = [a b c d; a e c f]; % To make h = [b c d; e c f]

Accepted Answer

Bruno Luong
Bruno Luong on 23 Aug 2020
Edited: Bruno Luong on 23 Aug 2020
h = randi(2,2,20)
% remove columns that are consecutively repeated
lgt = diff(find([true, any(diff(h,1,2)~=0,1), true]));
h = h(:,repelem(lgt==1,lgt))
  3 Comments
Bruno Luong
Bruno Luong on 23 Aug 2020
Edited: Bruno Luong on 23 Aug 2020
"line of code only removes adjacent columns"
Yes, it's not what you want?
If you want to remove any column that appears at least twice in h, regardless where it is:
a=1; b=2; c=3; d=4; e=5; f=6;
h = [a b c b;
b d f d]
code
[~,~,J]=unique(h.','rows');
b=accumarray(J,1)>1;
h(:,b(J))=[]
If not then you need to explain more what you want.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!