finding same values in matrix

1 vue (au cours des 30 derniers jours)
nkumar
nkumar le 15 Mar 2013
I have a matrix of size A=31x80,with 1st row representing the values 1 to 30,next i have another matrix C=30x81, i want to find in which row the matrix values C are present in A
  1 commentaire
Image Analyst
Image Analyst le 15 Mar 2013
Modifié(e) : Image Analyst le 15 Mar 2013
Are these integers, or floating point numbers? (See the FAQ.) What do you mean "which row"? A will have a bunch of numbers, and C will have a bunch of numbers and the locations of them in A will be scattered around - it won't just be one row or one (row, column) combination. C will not fit in A so you can't find the upper left corner of C where it fits as an intact whole matrix in A.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 15 Mar 2013
Modifié(e) : Andrei Bobrov le 15 Mar 2013
A = randi(124,31,80);
C = randi(50,30,81);
cu = unique(C);
[aa,ii] = ismember(A,cu);
[i1,j1] = find(aa);
out = [cu(ii(aa)),i1,j1];
ADD on nkumar's comment in Image Analyst's answer
[aa,ii] = ismember(A(:,2:end),C,'rows');
  2 commentaires
Andrei Bobrov
Andrei Bobrov le 15 Mar 2013
see ADD in my answer
Image Analyst
Image Analyst le 15 Mar 2013
Modifié(e) : Image Analyst le 15 Mar 2013
nkumar: Not sure why you accepted this when it does not work with the sample data you gave:
% Generate nkumar's sample data.
A=[1 0.2 0.6 0.3 0.5
2 0.6 0.5 0.3 0.1
3 0.1 0.2 0.3 0.4
30 0.2 0.4 0.6 0.8]
C=[0.6 0.5 0.3 0.1]
cu = C;
[aa,ii] = ismember(A,cu);
[i1,j1] = find(aa);
out = [cu(ii(aa)),i1,j1];
% ADD on nkumar's comment in Image Analyst's answer
[aa,ii] = ismember(A(:,2:end),C,'rows');
Error Message:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in test (line 20)
out = [cu(ii(aa)),i1,j1];
Did you fix the error? Or did you not get it for some reason?

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 15 Mar 2013
Try this:
% Generate sample data.
A = randi(9, 31, 80)
C = randi(5, 30, 81)
% Get list of unique numbers.
numbersInA = unique(A)
numbersInC = unique(C)
% Find where numbers in C occur in A
for k = 1 : length(numbersInC)
fprintf('Locations of %d in A:\n', numbersInC(k));
% Find rows and columns where this number occurs in A
[rowA colA] = find(A == numbersInC(k))
end
  3 commentaires
Image Analyst
Image Analyst le 15 Mar 2013
Modifié(e) : Image Analyst le 15 Mar 2013
OK, so they're floating point, not integers, and they're different sizes, which means you can't just subtract the matrices, and you may not be able to use the unique() function if these numbers were generated from some kind of calculation rather than just hard coded into the m-file.
If you have the Image Processing Toolbox, you can do it in one line: a call to the normalized cross correlation function.
% Generate sample data.
A=[1 0.2 0.6 0.3 0.5
2 0.6 0.5 0.3 0.1
3 0.1 0.2 0.3 0.4
30 0.2 0.4 0.6 0.8]
C=[0.6 0.5 0.3 0.1]
% Now compute the normalized cross correlation using
% normxcorr2() in the Image Processing Toolbox.
cc = normxcorr2(C, A)
% Find out the maximum value of the cross correlation.
maxValue = max(cc(:))
% Find row and column where C is centered over matching part of A.
% That is, where the normalized cross correlation is maximum.
[maxRow, maxCol] = find(cc == maxValue)
% The row is the same because C is just one row, but if we want the
% starting column, we have to shift the location.
maxCol = maxCol - size(C, 2) + 1;
fprintf('The upper left corner of C occurs at row = %d, column = %d of matrix A.\n',...
maxRow, maxCol);
nkumar
nkumar le 15 Mar 2013
Thanks a lot

Connectez-vous pour commenter.


Youssef  Khmou
Youssef Khmou le 15 Mar 2013
Modifié(e) : Youssef Khmou le 15 Mar 2013
hi, try for example :
A=rand(80,31);
A(1,:)=1:31;
C=rand(80,31);
Diff=abs(C-A);
[x,y]=find(Diff==0);
each value in both C and A has coordinates x,y , then x and y has the same length.
  1 commentaire
Image Analyst
Image Analyst le 15 Mar 2013
You didn't use the matrix sizes he gave: 31x80 and 30x81. His sizes are different so you can't simply subtract the matrices. And your method only would work where the numbers in C are in exactly the same location in A, not if the numbers occur in different locations.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by