How to loop identical numbers and find row numbers
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rashmi Mohan Kumar
el 25 de Nov. de 2019
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 comentarios
Adam Danz
el 25 de Nov. de 2019
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?
Respuesta aceptada
Adam Danz
el 25 de Nov. de 2019
Editada: Adam Danz
el 25 de Nov. de 2019
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!