Asked by 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

Answer by Andrei Bobrov
on 15 Mar 2013

Edited by Andrei Bobrov
on 15 Mar 2013

Accepted Answer

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');

Andrei Bobrov
on 15 Mar 2013

see ADD in my answer

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?

Log in to comment.

Answer by 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

nkumar
on 15 Mar 2013

my question is if

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]

it must represent the values are present in 2nd column

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
on 15 Mar 2013

Thanks a lot

Log in to comment.

Answer by Youssef Khmou
on 15 Mar 2013

Edited by 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.

Image Analyst
on 15 Mar 2013

*exactly the same location* in A, not if the numbers occur in different locations.

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 1 Comment

## Image Analyst (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/67308-finding-same-values-in-matrix#comment_136673

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 Aso you can't find the upper left corner of C where it fits as an intact whole matrix in A.Log in to comment.