find function on specific row and column of an array.

22 Ansichten (letzte 30 Tage)
JohnDylon
JohnDylon am 29 Okt. 2016
Bearbeitet: Image Analyst am 30 Okt. 2016
Hi,
I used find function in order to find row location of an array cell. To illustrate in an example:
A=magic(10)
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
find(A(4:5,4)==21)
ans =
1
What I wanted to get is in intersection of a row window and a specific column, I wanted to find a specific input's row location in the matrix. I expected to find the row number as 4, however I get 1. Find function thinks A(4:5,4) is a new array and gives locations accordingly.
Does anyone know how to get the true row location when I specify an mxn sub-matrix in a rxs matrix where r>m, s>n?
Thx,
JD

Akzeptierte Antwort

Image Analyst
Image Analyst am 30 Okt. 2016
Bearbeitet: Image Analyst am 30 Okt. 2016
To find a submatrix in another matrix, use ismember:
m = magic(10)
subm = m(4:6, 3:8)
% c = normxcorr2(subm, m)
[ia, ib] = ismember(m, subm)

Weitere Antworten (1)

Image Analyst
Image Analyst am 29 Okt. 2016
Just don't specify a sub-matrix. Why are you doing that? It just ruins it and doesn't give you what you want. Just do it like this:
A=magic(10)
[rows, columns] = find(A == 21)
  4 Kommentare
JohnDylon
JohnDylon am 29 Okt. 2016
Bearbeitet: JohnDylon am 29 Okt. 2016
Before code finds its location, I don't know it is at (9,4) coordinate in the array. I expect the code finds it, I just told it in order to make my problem clear.
Matrix A is like
A=
1 24 1 1 15
2 5 1 2 16
3 6 1 3 22
4 12 1 4 3
5 18 1 5 9
6 13 2 1 1
7 20 2 2 13
8 5 2 2 8
9 14 2 3 19
10 6 2 4 1
11 28 3 1 22
12 25 3 2 23
13 20 3 3 21
14 21 3 4 26
15 27 3 5 27
It has a sub matrix
A1=
6 13 2 1 1
7 20 2 2 13
8 5 2 2 8
9 14 2 3 19
10 6 2 4 1
and I wanted to find row location of A(7,4) however I didn't know it was at the 7th row at first place, I needed that info.
I guess find still can work for this specific problem with
row=find(A(:,3)==2 & A(:,4)==2, 1, 'first')
that generates row=7 and
row=find(A(:,3)==2 & A(:,4)==2, 1, 'last')
that generates row=8.
Thank you for help.
JD
Image Analyst
Image Analyst am 29 Okt. 2016
So you want to do basically a template matching. You have a known submatrix and want to find its location inside a larger matrix. You can use normxcorr2() to do that. I attach a demo.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by