How to find the indices of similar arrays

10 次查看(过去 30 天)
Dear Matlab expertises
I have a large matrix that has some repeated arrays. From this large matrix, I generated a smaller matrix with all unique arrays. Now for each unique array, I would like to find the indices of all similar arrays in the large matrix. I know I may need to use a loop by reading off each unique array in the small matrix and comparing it to the arrays in the large matrix. But the question is how to find the indices of these similar arrays.
Sorry for the long question.

采纳的回答

David Hill
David Hill 2022-2-16
Do not understand your reference to arrays. Are you talking about row or columns in your matrix? I will assume you are talking about rows and the number of column between large and small matrices are the same.
lmatrix=randi(3,10000,5);
smatrix=unique(randi(3,10,5),'rows');
idx=[];
for k=1:size(smatrix,1)
idx=[idx,find(ismember(lmatrix,smatrix(k,:),'rows'))'];
end
  3 个评论
David Hill
David Hill 2022-2-17
Above should be correct. idx will give you the index of the rows in the large matrix that are the same as the small matrix.
Abdulaziz Abutunis
Abdulaziz Abutunis 2022-2-23
Thank you David,
This works well. The only thing is changed is
idx=[idx,find(ismember(lmatrix,smatrix(k,:),'rows'))']; to
idx=[find(ismember(lmatrix,smatrix(k,:),'rows'))'];
I removed idx from the right hand side becuase I wanted to separate the indeces of rows with unique values

请先登录,再进行评论。

更多回答(1 个)

Abolfazl Chaman Motlagh
The function unique return both unique array of input array and the indexes of original array in unique array.
for example :
A = randi([1 5],5,5) % a 5x5 array with just number 1,2,3,4,5
A = 5×5
3 1 2 4 4 4 1 1 3 4 5 1 3 1 4 1 1 1 5 4 3 5 2 1 1
[Unique_A,Unique_index,Indexes] = unique(A);
the Unique_A in above is array of unique elements of A. the Unique_index is index of A which are in Unique_A. and Indexes is index of Unique_A for each elements in A. in other word:
Unique_A = A(Unique_index) and A(:) = Unique_A(Indexes)
so for findind index you can just search in these elements :
Indexes_of_A = Indexes' == Unique_A
Indexes_of_A = 5×25 logical array
0 0 0 1 0 1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
by this operation Indexes_of_A is in this example a 5x25 array with in row i, if j-th elements of A ( means A(j)) is i, it is 1, otherwise it is 0. so we can find the linear indexes:
linear_index = cell(numel(Unique_A),1);
all_ind = 1:numel(A); % 1,2,...,25
for i=1:numel(Unique_A)
linear_index{i} = all_ind(Indexes_of_A(i,:));
end
linear_index
linear_index = 5×1 cell array
{[4 6 7 8 9 12 14 18 20 25]} {[ 11 15]} {[ 1 5 13 17]} {[ 2 16 21 22 23 24]} {[ 3 10 19]}
you can see for example 11,15 (second row) are linear index of those elements in A which are Unique_A(2) which is 2 here.
you can change the linear index to index of matrix using ind2sub. for example use this instead of last part:
linear_index = cell(numel(Unique_A),1);
all_ind = 1:numel(A); % 1,2,...,25
for i=1:numel(Unique_A)
[row,col]= ind2sub(size(A),all_ind(Indexes_of_A(i,:)));
linear_index{i} = [row' col'];
end
linear_index
linear_index = 5×1 cell array
{10×2 double} { 2×2 double} { 4×2 double} { 6×2 double} { 3×2 double}
now the linear_index{i} is nx2 matrix, the first column are rows and second are cols of indexes.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by