Counting and removing rows with the same numbers in a particular order
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Rik
am 27 Mai 2020
Bearbeitet: Rik
am 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 Kommentare
Rik
am 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
am 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.
the cyclist
am 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);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!