Find index of a matrix of values into another matrix

조회 수: 15 (최근 30일)
Georgios Tertikas
Georgios Tertikas 2019년 4월 15일
댓글: Jon 2019년 4월 15일
Hello,
I have a matrix A 151x1 double and a matrix B 151x1000 double. Each value of A corresponds to a certain value in the same row of B. For example if A=22;14;53;2, then B will have the value 22 in one of its columns in the first row, the value 14 in one of its columns in the second row etc. Is there a way to have the matrix of the index of all the columns in B that contain the values of A (always in their coresponding row?).
  댓글 수: 3
Georgios Tertikas
Georgios Tertikas 2019년 4월 15일
if A = 1;5;15;22 and B= 31 23 10 1 9; 221 31 51 5 11; 15 22 33 44 55; 20 21 22 23 24
then I want a matrix that will be C=4;4;1;3 (for each position in B)
madhan ravi
madhan ravi 2019년 4월 15일
See my answer below.

댓글을 달려면 로그인하십시오.

채택된 답변

madhan ravi
madhan ravi 2019년 4월 15일
Indices = cell(numel(A),1);
for k = 1:numel(A)
Indices{k} = find(B(k,:)==A(k));
end
%celldisp(Indices)
% Indices{1} represents first element of A match in first row of B likewise others
  댓글 수: 9
Adam Danz
Adam Danz 2019년 4월 15일
Now test it with these inputs and you'll see why the simpler suggestion doesn't work.
A = [1;5;15;22]
B = [1 23 10 1 9; 1 31 51 5 11; 15 22 33 44 55; 20 5 22 22 22]
Jon
Jon 2019년 4월 15일
Yes I agree your approach will cover the case where there is more than one matching entry in a row. I did condition my original response with limitation that for the simple approach to work there had to be exactly one match in each row. In the end I'm not sure whether the OP has multiple matching entries in a row. Thanks for the clarification and discussion.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Adam Danz
Adam Danz 2019년 4월 15일
If I understood you correctly, you have a matrix "B" and a column vector "A" and you want the identify the columns of B that are identical to A (if any). Here's a demo that achieves that:
% Create data
A = randi(100, 151,1);
B = randi(100, 151, 1000);
B(:,55) = A; %column #55 of B equals A
B(:,66) = A; %column #66 of B also equals A
% Identify columns of B that match A
% matchIdx is a logical row vectors containing 'true' for columns of B that match A
matchIdx = ismember(B.', A.', 'rows').';
% Find the column numbers
colNums = find(matchIdx);
colNums =
55 66
  댓글 수: 2
Georgios Tertikas
Georgios Tertikas 2019년 4월 15일
So indeed I want to find which column in B has the same value as A but not with A as a whole matrix. I want for the first row (where A has one value and B has 1000) to find which column of B has the same value. And then do this also for the other 151 rows each time specifically for each row.
Adam Danz
Adam Danz 2019년 4월 15일
madhan ravi's solution should do what you're describing. If not, you can elaborate.

댓글을 달려면 로그인하십시오.


Jon
Jon 2019년 4월 15일
This should work assuming you have exactly one match in each row. If some rows have no matches or more than one match you will have to do something more elaborate
% find the row and column indices of all the places in B that match A
[row,col] = find(A==B)
% sort the matching column locations by row to get them in the desired (row) order
idx = col(row)
  댓글 수: 3
Jon
Jon 2019년 4월 15일
편집: Jon 2019년 4월 15일
Actually, it is ok the == operation apparently operates column wise in this situation and then find with two left hand arguments works perfectly for this situation. Please try it to see.
For more info on A==B with A a column with the same number of rows as B please see https://www.mathworks.com/help/matlab/matlab_prog/compatible-array-sizes-for-basic-operations.html
Adam Danz
Adam Danz 2019년 4월 15일
The problem is not that A and B are not the same size. The problem is that this does not address the question. The OP wants to list column numbers where row 'n' of B matches the value of row 'n' of A.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by