Choosing data from an array using a binary array

23 views (last 30 days)
I have a matrix Z and a same-sized binary array. I want to 'filter' the Z values through this binary array, so that I get the values of Z only where the ones are. Both arrays are double. I am doing: Z.*binary it seems to work, but when I surf the result, it's all weird. Why?

Answers (5)

Azzi Abdelmalek
Azzi Abdelmalek on 18 Jul 2015
You can also use
Z (logical (binary))
  1 Comment
Maria
Maria on 19 Jul 2015
This option produces a vector, though, and I am interested in maintaining the matrix form. I want to surf the results and compare them with the entire Z array.

Sign in to comment.


Image Analyst
Image Analyst on 19 Jul 2015
Azzi's solution is the only way to get "the values of Z only where the ones are". But you say you don't want that. You say you are "interested in maintaining the matrix form". So the way to do that is to do
output = z .* binaryArray;
That will maintain the shape and dimensions of the z array but you will get more values than only the values where the binary array is 1. Where the binary array is 0, you will get zeros. You cannot have it both ways. If z is to be the same shape then there has to be something where the binary array is 0. It has to be zero, nan, or (if a cell array) then null, but it has to be something - they can't just be "missing".
Why don't you give us the larger picture and context of what you really want to accomplish and we can tell you the best way to get there. You're asking how to do some impossible contradictory thing but, even if you were able to do it, that may not be the best way to get what you ultimately want.
  3 Comments
Image Analyst
Image Analyst on 19 Jul 2015
OK, so you're getting ALL the z values, not just "the values of Z only where the ones are", you're also getting the values where the zeros are (and of course those values are also zero because they got multiplied by zero). Why do you think the y values are getting reversed? Maybe the perspective is just not what you thought it was. Why don't you try labeling the axes so you know what each axis is:
xlabel('X', 'FontSize', 24);
ylabel('Y', 'FontSize', 24);
zlabel('Z', 'FontSize', 24);
Then see. Otherwise maybe you're confusing how matrices and images have the y values (1 at the top and increasing as you move down) with how plots in Cartesian coordinates have it (y=0 at the bottom and increasing as you move up the page). They're opposite directions by convention so you just need to know what interpretation to use.
Walter Roberson
Walter Roberson on 19 Jul 2015
When I test with some artificial data, I do not observe any reversal of Y axis.

Sign in to comment.


Walter Roberson
Walter Roberson on 19 Jul 2015
You might want to try
surf(Z, 'alphadata', double(binary), 'FaceAlpha', 'interp')

Cedric
Cedric on 19 Jul 2015
Edited: Cedric on 19 Jul 2015
(Formerly a comment) What do you want the points that are not flagged by your logical array to be replaced with?
Example:
figure ;
set( gcf, 'Units', 'normalized', 'Position', [0.2, 0.4, 0.6, 0.4] ) ;
[X, Y, Z] = peaks( 25 ) ;
hAxis1 = subplot( 1, 2, 1 ) ;
surf( X, Y, Z ) ;
isLe2 = Z <= 2 ; % Array of logicals.
Z_trunc = Z ; % Create copy that will be updated.
Z_trunc(~isLe2) = NaN ; % NaN elements not flagged by isLe2.
subplot( 1, 2, 2 ) ;
surf( X, Y, Z_trunc ) ;
zlim( zlim( hAxis1 )) ; % Match zlim.
caxis( caxis( hAxis1 )) ; % Match colors.
  1 Comment
Maria
Maria on 19 Jul 2015
Just the original content of the Z. That's why simple multiplication should do it. But the Y gets reversed. I'll check out the example and comment again tomorrow.

Sign in to comment.


Maria
Maria on 19 Jul 2015
Thank you all. The above are all good options. The problem I had has to do with the way my binary file is generated from an image and the Yaxis is reversed in the process. However, the binary plot looks as it should with this reversal when using imshow. I need to flip it before operating on it. Thanks again.

Community Treasure Hunt

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

Start Hunting!