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
  4 个评论
Spencer Giglio
Spencer Giglio 2020-5-27
It is all one digit numbers and they are stored as numeric values within a matrix.
Spencer Giglio
Spencer Giglio 2020-5-27
The row is anywhere from 3 to 17 columns long depending on what size I need to analyze.

请先登录,再进行评论。

回答(2 个)

Rik
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
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
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
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);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by