Effacer les filtres
Effacer les filtres

Find the switching point of a binary matrix

2 vues (au cours des 30 derniers jours)
EM
EM le 12 Mai 2016
Commenté : EM le 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.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by