Counting and removing rows with the same numbers in a particular order

2 vues (au cours des 30 derniers jours)
Spencer Giglio
Spencer Giglio le 27 Mai 2020
Commenté : the cyclist le 28 Mai 2020
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 commentaires
Spencer Giglio
Spencer Giglio le 27 Mai 2020
It is all one digit numbers and they are stored as numeric values within a matrix.
Spencer Giglio
Spencer Giglio le 27 Mai 2020
The row is anywhere from 3 to 17 columns long depending on what size I need to analyze.

Connectez-vous pour commenter.

Réponses (2)

Rik
Rik le 27 Mai 2020
Modifié(e) : Rik le 27 Mai 2020
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 commentaires
Rik
Rik le 27 Mai 2020
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 le 28 Mai 2020
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.

Connectez-vous pour commenter.


the cyclist
the cyclist le 27 Mai 2020
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);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by