Removing adjacent duplicate numbers in rows of a matrix
Show older comments
Hi,
assume the following matrix format:
xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 5 5 3 3 2]
I want to remove the adjacent duplicate numbers in each row of this matrix so I get the following output:
result=[1 2 3 4; 1 2 3 ; 5 3 2]
Help would be very much appreciated with a formula. Thanks.
Answers (2)
infinity
on 21 Jun 2019
Hello,
In your problem, you may get error since your result is not a matrix (number of colume of the first row is different with other rows).
So, you result should be a cell like this
clear
xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 5 5 3 3 2]
for i = 1:size(xx,1)
yy{i} = unique(xx(i,:));
end
12 Comments
AA
on 22 Jun 2019
Rik
on 22 Jun 2019
That is not possible in Matlab. Arrays must be rectangular, so every row must have the same number of columns. You must either pad each row with some value (e.g. NaN), or use a cell array.
infinity
on 22 Jun 2019
Hello @AA
Like @Rik said it is not allowed in Matlab. However, we can pass through it with small trick.
Let say, we can create the matrix that has the same number of rows and put the value of elements that are not in "xx" by a fake number, for example 0 or 1000.
Here is a reference for you
clear
xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 5 5 3 3 2]
yy = 0*xx;
for i = 1:size(xx,1)
temp = unique(xx(i,:));
yy(i,1:length(temp)) = temp;
end
AA
on 22 Jun 2019
infinity
on 22 Jun 2019
Hello,
What do you mean by removing the zeros?
Is it removing zeros in matrix "yy" to create another matrix?
AA
on 22 Jun 2019
When we remove 0 in "yy", the number of rows of the new matrix should be the same. The smallest size matrix that we can be obtained by
zz =
1 2 3 4
1 2 3 0
2 3 5 0
AA
on 22 Jun 2019
infinity
on 22 Jun 2019
Here you can refer this
clear
xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 5 5 3 3 2]
yy = 0*xx;
for i = 1:size(xx,1)
temp = unique(xx(i,:),'stable');
yy(i,1:length(temp)) = temp;
end
Rik
on 22 Jun 2019
I would suggest using the code below instead. With a NaN you avoid confusion if the last value happens to be a 0.
clear
xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 5 5 3 3 2]
%yy = zeros(size(xx));
yy=NaN(size(xx))
for row = 1:size(xx,1)
temp = unique(xx(row,:),'stable');
yy(row,1:length(temp)) = temp;
end
infinity
on 23 Jun 2019
Hello,
You can modify the code like this
clear
% xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 5 5 3 3 2]
% xx=[1 2 3 3 4 4; 1 1 2 2 3 3 ; 5 4 5 3 3 2]
xx=[1 2 3 3 4 4 3; 1 1 2 2 3 3 2; 5 5 5 3 3 2 5]
% xx=[1 2 3 3 4 4 3 3; 1 1 2 2 3 3 2 3; 5 5 5 3 3 2 5 5]
n = size(xx,2);
% yy = 0*xx;
yy=NaN(size(xx));
for i = 1:size(xx,1)
[temp,ia,ib] = unique(xx(i,:),'stable');
f = diff(ib)~=0;
idx = [find(f);length(ib)];
% yy(i,1:length(temp)) = temp;
yy(i,1:length(idx)) = xx(i,idx);
end
yy
Rik
on 22 Jun 2019
You can use diff to find any repeats and remove them. The compare to eps is to avoid strange things happening once you put in decimal numbers, which can lead to rounding errors, which is why exp(log(3))==3 returns false.
xx=[1 2 3 3 4 4 3; 1 1 2 2 3 3 2; 5 5 5 3 3 2 5];
yy=NaN(size(xx));
for row = 1:size(xx,1)
temp = xx(row,:);
%keep values that are different from the one before
%L=[true diff(temp)~=0];%line below is safe for float rounding
L=[true abs(diff(temp))>=(2*eps)];
yy(row,1:sum(L)) = temp(L);
end
%remove trailing NaN cols:
while all(isnan(yy(:,end)))
yy(:,end)=[];
end
disp(yy)
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!