finding same values in matrix

19 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2013-3-15
编辑:Image Analyst 2013-3-15
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.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-3-15
编辑:Andrei Bobrov 2013-3-15
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 个评论
Image Analyst
Image Analyst 2013-3-15
编辑:Image Analyst 2013-3-15
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?

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2013-3-15
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 个评论
Image Analyst
Image Analyst 2013-3-15
编辑:Image Analyst 2013-3-15
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);

请先登录,再进行评论。


Youssef  Khmou
Youssef Khmou 2013-3-15
编辑:Youssef Khmou 2013-3-15
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 个评论
Image Analyst
Image Analyst 2013-3-15
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.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by