Counting and removing rows with the same numbers in a particular order
2 次查看(过去 30 天)
显示 更早的评论
I have a data set with purmutation differences. I want to find all the rows that have the same numbers in the same order but potentially not in the same column. For example:
A = [1234;4123;3412]
would be considered the same and I would want to count these three rows and then delete two of them.
If this does not make sense please let me know, but I appreciate any help I can get. Thank you
回答(2 个)
Rik
2020-5-27
编辑:Rik
2020-5-27
Assuming this is indeed the input data you have:
A = [1234;4123;3412];
B=arrayfun(@(x) sort(sprintf('%d',round(x))),A,'UniformOutput',0);
C=unique(B);
D=cellfun(@str2double,C);
Either C or D should be what you want.
Edit:
If you have an array of one digit numbers you can skip the conversion to char and back:
A = [1 2 3 4;4 1 2 3;3 4 1 2];
D = unique(sort(A,2),'rows');
3 个评论
Rik
2020-5-27
If you look at the documentation for the unique function you will see that there is a way to convert back with the indices. These indices can be used with histcounts.
The 2 in the call to sort is refering to which dimension sort should operate on, as you could have read in its documentation.
My code was predicated on the assumption that order didn't matter. I need to have a longer look at this to find a solution. You can probably use circshift on each row until some metric is true (e.g. the lowest value is at the front). Such a metric would allow you to still use unique.
the cyclist
2020-5-28
Spenser, there is a lesson here for you. It's better to think a bit more carefully about your question, and provide all the detail upfront. You would have saved everyone, including yourself, a lot of time.
the cyclist
2020-5-27
Pending answers to the questions in the comments, something like this will work on your input as written:
A = [1234;4123;3412];
[~,indexToUniqueA] = unique(sort(num2str(A),2),'row');
uniqueSortedA = A(indexToUniqueA);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!