Problem 320. Maintain shape of logical-index mask

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]);



returns the output

out =

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

48.62% Correct | 51.38% Incorrect
Last solution submitted on Nov 13, 2016

Problem Comments

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!

Join the 15-year community celebration.

Play games and win prizes!

Learn more