finding same values in matrix
1 回表示 (過去 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
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
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
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
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
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
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
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 Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!