How to remove repeating rows without unique function?

조회 수: 6 (최근 30일)
Andrew Poissant
Andrew Poissant 2017년 9월 25일
댓글: Cedric 2017년 9월 26일
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.

채택된 답변

Cedric
Cedric 2017년 9월 25일
편집: Cedric 2017년 9월 25일
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
Andrew Poissant
Andrew Poissant 2017년 9월 26일
It worked! Thank goodness. Really appreciate all of the help you gave me, you rock!
Cedric
Cedric 2017년 9월 26일
My pleasure. The amount of unsupported features seems to be a real pain, good luck with the rest!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2017년 9월 26일
편집: Jan 2017년 9월 26일
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

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by