フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by