Find the switching point of a binary matrix
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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)
更多回答(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!