Maintain shape of logical-index mask - MATLAB Cody - MATLAB Central

Problem 320. Maintain shape of logical-index mask

Difficulty:Rate

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
       2

But 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   2

In 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

37.17% Correct | 62.83% Incorrect
Last Solution submitted on Mar 31, 2025

Problem Comments

Solution Comments

Show comments
Registration Now Open for MathWorks AUTOMOTIVE CONFERENCE 2025
...
Hello Community, We're excited to announce that registration is now open for the...

Problem Recent Solvers71

Suggested Problems

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!
Go to top of page