How to loop identical numbers and find row numbers

10 次查看(过去 30 天)
Hi all,
I was actually working on a set of array elements, 50X2. The 1st column gives the frame numbers and the second column is the slot numbers. So each column as random numbers and they are repeating. So what I am trying to do in my experiment is as follows. For example I have the following matrix:
A= [ 1 3
4 7
1 6
4 7
1 6
5 3
4 7 ]
Now I need my code to check for all the repeating numbers in column 1. From the above matrix the values 1 and 4 repeats. For value 1, it repeats on row numbers 1, 3 and 5, I need the code to check for the corresponding values from the 2nd column but on the same rows, where value 1 is repeated in the 1st column. This has to be done for all the elements in column 1. After getting the corresponding numbers from column 2, the code should check if those numbers are also same, meaning from the matrix above for value 1 on row numbers 1, 3 and 5 the corresponding values from the 2ndcolumn are 3, 6 and 6. The code should check if these numbers are also same, if not ignore it and if it is same consider it only if it has repeated twice and if it as repeated more than twice then place a value equal to 1 on all those rows but in the 3rd column of the matrix. So basically it should ignore 3 and give the row numbers of the value 6, since it is repeated only twice that is row numbers 3 and 5, but if you see for a value of 4 in the 1stcolumn, the corresponding element in the 2nd column is 7 which even though is the same number it is repeated more than twice so it should be ignored and a value 1 should be placed on all the rows where it is 7 but in the 3rd column. And I wanted to implement this using looping instead of using functions for all the elements in the matrix.
Can anyone help me with this?
  3 个评论
Rashmi Mohan Kumar
Rashmi Mohan Kumar 2019-11-25
Hi Adam, thank you for your reply.
A= [ 1 3
4 7
1 6
4 7
1 6
5 3
4 7 ]
For all the 1's that are repeated in the 1st column, it should check for the corresponding elements on the 2nd column. If they are same then see if it is repeated only twice or more than twice. If it is repeated only twice then the code should give the row numbers of those elements and if it is repeated for more then twice ,in the 3rd column of the matrix A indicate it with a value 1 on the same rows. This looping should be done for all the elements of the 1st column.
Expected result
Since 1 is repeated on rows 1, 3 and 5, check for the corrresponding value in the 2nd column, which is 3, 6 and 6. Since 3 is a different number and since it occurs only once, ignore it but the number 6 occurs only twice, so get the row numbers of them (say store it in another variable B). But again if you see in column 1, the value 4 also repeats on rows 2, 4 and 7, but the corresponding elements from column 2 is 7 on all the rows and since it is repeated more then twice, the code should indicate this by a value 1 on the rows 2, 4 and 7 but in the 3rd column of matrix A.
so B=[3,5]
A= [ 1 3 0
4 7 1
1 6 0
4 7 1
1 6 0
5 3 0
4 7 1 ]
Any help will be very helpful!
Thank you.
Adam Danz
Adam Danz 2019-11-25
Ok, I've edited my answer to reproduce your expected output matrix.
In addition to that, you want another variable "B" that stores the row indices of A where there are exactly 2 (no more, no less) repeating rows. So, in this example, B would be B=[3,5]. Is that correct?

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-11-25
编辑:Adam Danz 2019-11-25
This finds all rows of A that are repeated at least two times and the labels them with a 1 in an appended third column.
A= [ 1 3
4 7
1 6
4 7
1 6
5 3
4 7 ];
AunqRows = unique(A,'rows','stable');
matchIdx = cell2mat(arrayfun(@(i)ismember(A,AunqRows(i,:),'rows'), 1:size(AunqRows,1), 'UniformOutput', false));
A(:,3) = any(matchIdx .* (sum(matchIdx,1)>2),2);
Result
A =
1 3 0
4 7 1
1 6 0
4 7 1
1 6 0
5 3 0
4 7 1
To find rows indices where there are exactly two matching rows,
B = find(any(matchIdx .* (sum(matchIdx,1)==2),2));
% ans =
%
% 3
% 5
  2 个评论
Rashmi Mohan Kumar
Rashmi Mohan Kumar 2019-11-25
Thank you for your helpful, but the code is not generating the row numbers at all. Kindly let me know if there are any changes I will have to make. I have also attached a snip of the output where even after the 1st and 2nd columns have same numbers the code does not generate the row numbers. For the row numbers 29 and 30 even though it satisfies the conditions the code does not generate their row numbers.
Capture.JPG
Adam Danz
Adam Danz 2019-11-25
编辑:Adam Danz 2019-11-25
Unfortunately I cannot copy-paste the data from the image. Could you provide those two columns of data in a copy-paste format?

请先登录,再进行评论。

更多回答(0 个)

类别

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