finding same values in matrix

36 views (last 30 days)
nkumar
nkumar on 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 Comment
Image Analyst
Image Analyst on 15 Mar 2013
Edited: Image Analyst on 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.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 15 Mar 2013
Edited: Andrei Bobrov on 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 Comments
Andrei Bobrov
Andrei Bobrov on 15 Mar 2013
see ADD in my answer
Image Analyst
Image Analyst on 15 Mar 2013
Edited: Image Analyst on 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?

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 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 Comments
Image Analyst
Image Analyst on 15 Mar 2013
Edited: Image Analyst on 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 on 15 Mar 2013
Thanks a lot

Sign in to comment.


Youssef  Khmou
Youssef Khmou on 15 Mar 2013
Edited: Youssef Khmou on 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 Comment
Image Analyst
Image Analyst on 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.

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!