필터 지우기
필터 지우기

Find the switching point of a binary matrix

조회 수: 2 (최근 30일)
EM
EM 2016년 5월 12일
댓글: EM 2016년 5월 13일
Hi all,
I have an nxn matrix (A) where all values are either 1 or 2. I have a second nxn matrix (B) with various numeric values. I would like to use matrix (A) to index into matrix (B) and extract only the data in (B) that corresponds to the points in matrix (A) where the value switches from 1 to 2. For example suppose A looks like the following 3x3.
[1 1 2;
1 2 2;
2 2 2]
And now suppose (B) looks like the following
[0.3562 0.1482 0.9850;
0.4296 0.1568 0.7623;
0.5966 0.5011 0.6814]
I am only interested in 0.5966, 0.1568, and 0.9850, so basically I'm looking for the diagonal line of 2's. However, there are not an equal number of observations of either side of the diagonal.
Any ideas would be appreciated.

채택된 답변

Image Analyst
Image Analyst 2016년 5월 12일
Try this with conv2() to sum up the elements and find where they equal 3:
A = [1 1 2;
1 2 2;
2 2 2]
B = [0.3562 0.1482 0.9850;
0.4296 0.1568 0.7623;
0.5966 0.5011 0.6814]
% Find 1 to 2 transitions in each direction.
c1 = conv2(A, [1,1], 'full') == 3;
c2 = conv2(A, [1;1], 'full') == 3
% Trim off ends and OR together.
mask = c1(:, 1:end-1) | c2(1:end-1, :)
% Extract the needed values
out = B(mask)
  댓글 수: 1
EM
EM 2016년 5월 13일
Brilliant! Thank you for your help! This works incredibly well.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by