Hi,
I needed a little help with finding the row numbers of similar elements in a matrix. It involves the use of unique,find and sort commnds but I could not clear the errors that I was facing. My code should do the following:
I have a matrix as follows:
1 3 2
2 4 1
4 3 6
5 1 8
2 8 9
1 7 5
6 6 2
7 9 1
1 3 2
I need my code to first check for similar values in column 1, for example in the above matrix let us consider for the number "1", it occurs three times in column1. So I want my code to check for all the rows in column 1 with a value 1 and then check for the same rows if the corresponding values in column 2 are also same, meaning in the 1st column above the value 1 occurs on rows 1, 6 and 9, then for the same rows the code should check what are the values in column 2, from the matrix it can be noticed that the corresponding values in column 2 for 1 is 3, 7 and 3. After this I need my code to check for the row numbers of similar elemnts that is 3. From the matrix above it is the row numbers 1 and 9. Then I need the code to check for the values in column 3 for rows 1 and 9, if the numbers in column 3 are same then set a variable to 1 otherwise 0. I need the code in a looping structure since I have to check for 50 values.
Any help or suggestions on how to do it is appreciated.
Thanks.

7 comentarios

KALYAN ACHARJYA
KALYAN ACHARJYA el 19 de Nov. de 2019
Illustration through example (and expected output) is much better than long text.
Rik
Rik el 19 de Nov. de 2019
So you need some code that finds duplicate rows?
Rik
Rik el 19 de Nov. de 2019
Comment posted as answer by Rashmi Mohan Kumar:
Hi all,
Thank you for your replies but both the answers posted gives the row numbers of the duplicates but I need to check for the repeated numbers, its value and the row numbers. Let me try to explain it with an example.
Consider the following matrix
A=[ 1 5 6
2 7 8
1 5 2
6 8 4
5 9 3
1 5 6
6 8 9
1 8 2
3 7 9 ]
Now I need the code to check for all the repeated numbers in column 1. In the 1st column, the numbers 1 and 6 are repeated. The value 1 is repeated on rows 1,3,6 and 8. The code should generate these row numbers and after this it should check for the corresponding elements in the 2nd column on the same rows on which the value 1 is repeated, that would be 5,5,5 and 8. The code should now perform the same thing as before that is again check for repeated numbers amongst these numbers that would be 5, 5 and 5, on rows 1, 3 and 6 and then check for the corresponding elements in the 3rd column on the same rows that will be 6,2 and 6. Again the code should check for repeated elements that will be 6 and 6 on rows 1 and 6. At this point the code should generate these row numbers and also set a variable to "1"
The same process should also repeat for the value 6 that is repeated in column 1, but once it comes to check for repeated numbers for column 2 there are no repeated numbers, the code should just end and come out of the loop.
Expected Ouputs:
repeated row numbers in column 3 for 1=[1,6]
w=1;
The process should be repeated for all the numbers repeated in column 1 and continue till the 3rd column, since like value 1 there can be other numbers also repeated till 3rd column, since I have an array of 50X3 elements.
Thank you.
I don't see your issue. Your could implement a recursive method (see below), but that doesn't actually change the outcome for the case you describe. See if the code below does what you mean, or try to find a counter-example of when your description should result in something other than the row indices of the repeated rows.
The only thing needed here is to check if the output is empty. If it isn't, set w to 1, otherwise, leave it 0.
A=[ 1 5 6
2 7 8
1 5 2
6 8 4
5 9 3
1 5 6
6 8 9
1 8 2
3 7 9 ];
find_repeated_row_indices(A)
find_repeated_row_indices2(A)
function out=find_repeated_row_indices(A,col,row_ind)
if nargin==1
col=1;
row_ind=1:size(A,1);
end
[a,~,c]=unique(A(row_ind,col),'stable');
hasDuplicates=find( 1 < accumarray([c ones(size(c))],1) );
out=0;%return 0 in case of no duplicates
for n=1:numel(hasDuplicates)
rows_next_layer= col & ...
A(:,col)==a(hasDuplicates(n));
if col==size(A,2)
%last col reached
temp_out=find(rows_next_layer)';
elseif ~any(rows_next_layer)
%no duplicates found
return
else
%continue to next column
temp_out=find_repeated_row_indices(A,col+1,rows_next_layer);
end
if ~isequal(temp_out,0)
out=temp_out;
end
end
end
function z=find_repeated_row_indices2(A)
[~,b,c]=unique(A,'rows','stable');
z=NaN( numel(c)-numel(b) +1 );%pre-allocate worst-case
hasDuplicates=find( 1 < accumarray([c ones(size(c))],1) );
%slower option:
%for n=reshape(b,1,[])
for n=reshape(hasDuplicates,1,[])
tmp=find(c==n);
if numel(tmp)~=1
z(n,1:numel(tmp))=tmp;
end
end
%remove NaN rows and cols
z(all(isnan(z),2),:)=[];
z(:,all(isnan(z),1))=[];
end
Thanks Rik, it was very helpful and it worked too. I had another question after generating the output rows instead of setting a variable to 1, is it possible to place a value 1 at the same rows that was generated but in the 4th column of A. For example, suppose the code generated [1,7] as the row numbers how do I a place a value 1 in both these rows but in the 4th column of A.
Happy to help. If my answer helped you, feel free to mark it as accepted answer.
As for your follow-up question: that would just be simple indexing.
A([1 7],end)=1;
Thanks again, but I did try implementing the same thing as you mentioned for the follow up question but it gave an error. I think is it because I am dealing with an 50X3 elements and here in the exmaple I have mentioned only for one repeated element but there can be row numbers generated too. Meaning to say the row numbers generated are[ 3, 7; 5, 1] then a 1 has to be placed at all these row numbers but in the 4th column of A.So should it be A([ans],4)=1; ??

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 19 de Nov. de 2019
Assuming you want a matrix with the duplicate row indices, the code below should work. It pads the columns with NaN. I slightly adapted your data to test the optimizations.
A=[1 3 2
2 4 1
2 4 1
2 4 1
4 3 6
5 1 8
2 8 9
1 7 5
6 6 2
7 9 1
1 3 2];
[a,b,c]=unique(A,'rows','stable');
z=NaN( numel(c)-numel(b) +1 );%pre-allocate worst-case
hasDuplicates=find( 1 < accumarray([c ones(size(c))],1) );
%slower option:
%for n=reshape(b,1,[])
for n=reshape(hasDuplicates,1,[])
tmp=find(c==n);
if numel(tmp)~=1
z(n,1:numel(tmp))=tmp;
end
end
%remove NaN rows and cols
z(all(isnan(z),2),:)=[];
z(:,all(isnan(z),1))=[];
clc
if ~isempty(z)
disp(z);
else
disp('nothing is found');
end

Más respuestas (1)

Vladimir Sovkov
Vladimir Sovkov el 19 de Nov. de 2019

0 votos

I think that the code in the attachment can help.

Etiquetas

Preguntada:

el 19 de Nov. de 2019

Comentada:

el 20 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by