How to remove repeating rows without unique function?

I have a 624x2 matrix of ordered pairs, of which there are some repeating rows that I must remove. Is there a way to remove these repeating rows without using the unique function? Maybe using a for loop? The reason I can't use the unique function is because when I implement this matlab script in Simulink's Matlab Function block, the matrix must be sorted first before using the unique function. I can't sort the matrix because I will lose my ordered pairs and sortrows doesn't solve the issue either because I get the same error from Simulink.

 Respuesta aceptada

Cedric
Cedric el 25 de Sept. de 2017
Editada: Cedric el 25 de Sept. de 2017
Use the 'stable' option:
>> A = randi( 3, 10, 2 )
A =
2 3
1 1
3 1
3 1
3 1
3 3
3 3
2 1
2 3
1 1
>> unique( A, 'rows' )
ans =
1 1
2 1
2 3
3 1
3 3
>> unique( A, 'rows', 'stable' )
ans =
2 3
1 1
3 1
3 3
2 1

8 comentarios

I have tried using the stable option and am still getting the errror "The second operand is not sorted in ascending order. Use SORT first" in Simulink. The second operand is referring to my unique function call
Cedric
Cedric el 25 de Sept. de 2017
Editada: Cedric el 25 de Sept. de 2017
I had never seen that. Here is another approach:
>> A = randi( 3, 10, 2 )
2 1
2 3
3 2
3 1
1 1
2 2
2 3
2 2
3 2
3 1
>> [A_s, id_s] = sortrows( A ) ;
[~, id_u] = unique( A_s, 'rows' ) ; % Passing sorted A to UNIQUE.
A_u = A(sort( id_s(id_u) ), :)
which outputs
A_u =
2 1
2 3
3 2
3 1
1 1
2 2
and just to check:
>> isequal( A_u, unique( A, 'rows', 'stable' ))
ans =
logical
1
So that seems to be working.
Note that Simulink users may know a better approach and/or why you seem to have to sort your data before you can use UNIQUE, because I have no clue!
It is still giving me the same error. Sortrows does not work for sorting, Simulink needs the function SORT used for this. Is there any way to remove repeating rows without using unique at all?
Cedric
Cedric el 26 de Sept. de 2017
Editada: Cedric el 26 de Sept. de 2017
It would be better if a Simulink specialist would kick in and explain how this can be done properly in Simulink. I can only find "tricks" to emulate what you don't seem to have.
Here is another one:
>> A = randi( 3, 10, 2 )
A =
3 1
3 3
1 3
3 2
2 3
1 1
1 2
2 3
3 3
3 3
>> checkFun = @(rowId) ~any(all(bsxfun(@eq, A(rowId,:), A(1:rowId-1,:)), 2)) ;
select = [true, arrayfun(checkFun, 2:size(A, 1))] ;
A_u = A(select, :)
which outputs:
A_u =
3 1
3 3
1 3
3 2
2 3
1 1
1 2
and to check:
>> isequal( A_u, unique( A, 'rows', 'stable' ))
ans =
logical
1
Yes, that would be nice if someone versed in simulink could offer advice, but I appreciate your help. Unfortunately, Simulink matlab function block can't take function handles or use arrayfun either. It is extremely annoying how limited the block is
Cedric
Cedric el 26 de Sept. de 2017
Editada: Cedric el 26 de Sept. de 2017
Then there is the good ol' loop:
A = randi( 3, 10, 2 ) ;
nRows = size(A, 1) ;
keep = true(nRows, 1) ;
for rowId = 2 : nRows
if any(all(repelem(A(rowId,:),rowId-1, 1) == A(1:rowId-1,:), 2))
keep(rowId) = false ;
end
end
A_u = A(keep,:) ;
Test:
>> isequal(A_u, unique(A, 'rows', 'stable'))
ans =
logical
1
If REPELEM is not supported, replace by REPMAT. If REPMAT not supported either, we can build the darn matrix using a product.
It worked! Thank goodness. Really appreciate all of the help you gave me, you rock!
Cedric
Cedric el 26 de Sept. de 2017
My pleasure. The amount of unsupported features seems to be a real pain, good luck with the rest!

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 26 de Sept. de 2017
Editada: Jan el 26 de Sept. de 2017
I still do not understand, why you cannot simply sort the data. But this might be useful:
!!! UNTESTED !!!
function Data = UniqueRows2(Data)
nRow = size(Data, 1);
keep = true(nRow, 1);
for k1 = 1:nRow
if keep(k1)
for k2 = k1 + 1:nData
if keep(k2)
% For rows of length 2:
keep(k2) = (Data(k2, 1) ~= Data(k1, 1)) | (Data(k2, 2) ~= Data(k1, 2));
% General row length:
% keep(k2) = any(bsxfun(@ne, Data(k2, :), Data(k1, :)), 2);
end
end
end
end
Data = Data(keep, :);
end
This might be faster with vectorization:
function Data = UniqueRows(Data)
nRow = size(Data, 1);
keep = true(nRow, 1);
for k1 = 1:nRow
if keep(k1)
keep(k1 + 1:nData) = and(keep(k1 + 1:nData), ...
any(bsxfun(@ne, Data(k2, :), Data(k1, :)), 2));
end
end
Data = Data(keep, :);
end

Categorías

Más información sobre Simulink Functions en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Sept. de 2017

Comentada:

el 26 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by