# extract the intersection of two matrices

3 views (last 30 days)
ali eskandari on 24 Jul 2021
Edited: DGM on 24 Jul 2021
Hi
I have two matrices, one is binary (A) and other one with values between [0-1] (B). My aim is to extract the intersection of these two matrices. I did the following but I don't know why another area has been added to it.
Binary image:
imshow(A)
B:
imagesc(B)
A = double(A);
A(A==0) = -1; % Change zeros to -1 to keep the zero values in matrix B
A = A.*B;
caxis([0 1])
colormap jet
here is the output image, and some extra pixels have been selected as well.

DGM on 24 Jul 2021
Edited: DGM on 24 Jul 2021
I have a feeling that the range of data in B isn't what you think it is. Using the compositing method involving -1 is probably causing problems. If B contains very small negative values near zero, A.*B will result in those regions becoming positive. Then when you set your alpha data to A>=0, those regions show up.
Let me see if I can make an example.
B = repmat(linspace(-0.1,1,256),[256 1]); % the image (numeric)
A = B>0.4 & B<0.6; % the mask (logical)
A = double(A);
A(A==0) = -1; % Change zeros to -1 to keep the zero values in matrix B
C = A.*B;
caxis([0 1])
colormap jet
Note the extra area selected on the left. It's also possible that the issue isn't negative values in the image or mask, but simply the fact that A==0 will fail to select any near-zero (positive) values in a floating point copy of what should properly be a logical mask.
If you just want to mask off everything but what's defined by the (logical) mask, just use the mask.
B = repmat(linspace(-0.1,1,256),[256 1]); % the image (numeric)
A = B>0.4 & B<0.6; % the mask (logical)
colormap(jet)