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

조회 수: 2 (최근 30일)
Spencer Giglio
Spencer Giglio 2020년 5월 27일
댓글: the cyclist 2020년 5월 28일
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