Choosing data from an array using a binary array
23 views (last 30 days)
Show older comments
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?
0 Comments
Answers (5)
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
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
on 19 Jul 2015
When I test with some artificial data, I do not observe any reversal of Y axis.
Walter Roberson
on 19 Jul 2015
You might want to try
surf(Z, 'alphadata', double(binary), 'FaceAlpha', 'interp')
0 Comments
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.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!