When using logical indexing into an array, MATLAB returns a vector output. For example, if
A = magic(3);
mask = logical([0 1 1; 0 1 1; 0 1 1]);
then
out=A(mask)
returns the output
out =
1
5
9
6
7
2But when the mask has a block format (as in this example), it is sometimes desirable to maintain that block shape in the output. So, in this case, the desired output would be
out =
1 6
5 7
9 2In other words, the output is the same shape as the logical index block.
The (first) input array A will be two-dimensional, and the (second) input array mask will be a logical that is guaranteed to have the block characteristic.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers73
Suggested Problems
-
Swap the first and last columns
23187 Solvers
-
How to find the position of an element in a vector without using the find function
2822 Solvers
-
First non-zero element in each column
966 Solvers
-
318 Solvers
-
Sum of odd numbers in a matrix
622 Solvers
More from this Author23
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
More tests. A lot of solutions assume that mask has only 2 columns.
Good suggestion. I've added a couple more tests to the suite, including one that has three columns in the logical mask.
Is there a way to just view the solution to this? I've been trying to do this for my own work, and this problem is the only thing I've found that purports to do what I need, but of course it doesn't actually tell me how.