Find index of specific ordered pair in layered matrix
조회 수: 8 (최근 30일)
이전 댓글 표시
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
채택된 답변
Kevin Xia
2017년 8월 7일
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:
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!