Find index of specific ordered pair in layered matrix
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Andrew Poissant
il 7 Ago 2017
Modificato: Kevin Xia
il 9 Ago 2017
I have an ordered pair matrix, A=[x y], and a layered matrix, N, with three layers. What I want to do is search layer 2 for the correct x value and then layer 3 for the corresponding y value. Layer 2 represents the x axis and layer 3 represents the y axis. So in essence, I want to scan layer 2 and highlight the column that contains the x value and then scan the 3rd layer, highlight the row that contains the y value, and return the index at which layer 2 and layer 3 intersect. Any ideas on how to do this? I showed my setup of the layered matrix N and the values in matrix A. Layer 1 of matrix N are just random values and are irrelevant for this problem. Printing out layer 2 and 3 of matrix N would help visualize what I am talking about. Thanks!
cn = 3;
N11 = randi([0,5],cn,cn);
N12 = randi([1,4],cn,cn);
N13 = randi([0,7],cn,cn);
N14 = randi([0,6],cn,cn);
N21 = randi([1,3],cn,cn);
N22 = randi([1,7],cn,cn);
N23 = randi([4,6],cn,cn);
N24 = randi([2,7],cn,cn);
N31 = randi([1,4],cn,cn);
N32 = randi([6,7],cn,cn);
N33 = randi([1,7],cn,cn);
N34 = randi([3,4],cn,cn);
N41 = randi([2,5],cn,cn);
N42 = randi([0,3],cn,cn);
N43 = randi([5,6],cn,cn);
N44 = randi([0,4],cn,cn);
N = [N11 N12 N13 N14;
N21 N22 N23 N24;
N31 N32 N33 N34;
N41 N42 N43 N44];
[x, y] = size(N);
H1 = -x/2:1:x/2;
H2 = H1(find(H1~=0));
V1 = -y/2:1:y/2;
V2 = V1';
V3 = V2(find(V2~=0));
N(:,:,2) = H2(ones(x,1),:);
N(:,:,3) = V3(:,ones(y,1)) % finish making layered matrix, N
A = [-6 -1; -3 4; 2 2; 5 -1];
0 Commenti
Risposta accettata
Kevin Xia
il 7 Ago 2017
Suppose that you want to find the row and column index where the second layer is -6 (A(1,1)) and where the third layer is -1 (A(1,2)). To go about doing so, use:
[row col]=find(N(:,:,2)==A(1,1) & N(:,:,3)==A(1,2));
This will give you the row and column where the layers intersect. To understand why this is the case, it is necessary to break down the boolean expression into three parts:
1.) N(:,:,2)==A(1,1)
2.) N(:,:,3)==A(1,2)
3.) &
Part 1 generates a logical matrix. If a value of N(:,:,2) is -6, then value of the logical matrix at that index is 1. Otherwise it is 0. The following logical matrix is thus generated:
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
Part 2 also generates a similar logical matrix, only for the condition where N(:,:,3) is -1:
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
In part 3, the elementwise AND operator is used. The reason why the standard AND operator (&&) is not used is because && is used for scalar binary values. The elementwise AND (&) operator takes the corresponding element of both logical matrices and applies the AND operation. Thus, the following logical matrix is generated:
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!