Filter löschen
Filter löschen

Find the switching point of a binary matrix

2 Ansichten (letzte 30 Tage)
EM
EM am 12 Mai 2016
Kommentiert: EM am 13 Mai 2016
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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 12 Mai 2016
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 Kommentar
EM
EM am 13 Mai 2016
Brilliant! Thank you for your help! This works incredibly well.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by