Filter löschen
Filter löschen

How can I find indices?

2 Ansichten (letzte 30 Tage)
SM
SM am 27 Mai 2021
Beantwortet: Stephen23 am 27 Mai 2021
I have two matrices:
A=[1 2 3 3 1 1 2 1;
1 1 1 2 2 3 2 3];
B=[2 1;
1 3];
The output matrix will be either
indx=[2, 6];
or
indx=[0 1 0 0 0 1 0 0];
Is there any smart way? I can definately solve it by using loop and conditional statement.
Appreciated!
  2 Kommentare
KSSV
KSSV am 27 Mai 2021
What is output matrix?
SM
SM am 27 Mai 2021
Bearbeitet: SM am 27 Mai 2021
indx=[2, 6]; or indx=[0 1 0 0 0 1 0 0];
If you look at A and B, B(:,1) matches with A(:,2), and B(:,2) matches with A(:,6) and A(:,8). I need only one index of an column array. Thus, output should be indx=[2, 6].

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Mai 2021
A = [1,2,3,3,1,1,2,1;1,1,1,2,2,3,2,3]
A = 2×8
1 2 3 3 1 1 2 1 1 1 1 2 2 3 2 3
B = [2,1;1,3]
B = 2×2
2 1 1 3
[~,X] = ismember(B.',A.','rows')
X = 2×1
2 6

Weitere Antworten (1)

Image Analyst
Image Analyst am 27 Mai 2021
Explain to me how you got [2,6] because it's not obvious to me. Your tag says "matches" and to find B in A, you can do this:
A=[1 2 3 3 1 1 2 1;
1 1 1 2 2 3 2 3];
B=[2 1;
1 3];
[rowsA, colsA] = size(A);
[rowsB, colsB] = size(B);
counter = 1;
index = [];
for col = 1 : (colsA - colsB + 1)
subA = A(:, col : col + colsB - 1)
if isequal(subA, B)
index(counter) = k;
end
end
index
but you can see your B never appears anywhere in your A. Are you perhaps just looking at the top row of each? But even that will not work because [2,1] occurs only at column 7-8.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by