matrix with logicals and doubles

is it possible to create a matrix where part of it is defined as doubles and others as logicals?

 Accepted Answer

No. Matrices cannot have mixed types. You could use a cell array, like:
A = {true, 4;false, 5;true 9}
or have a matrix which holds both values to be used when needed.
A = [0 1 2 0;3 0 5 6;1 1 1 0]
% When a logical is needed:
LA = logical(A)

1 Comment

Note also that for the purposes of any logical test, values which are 0 (in any numeric data type) are considered false, and non-zero non-nan values are considered to be true. Thus, in many cases there is no need to explicitly use logical values at all.
logical vs double does make a difference for indexing. Also there are a number of routines that consider logical to signal black and white but consider double to signal relative intensities. The two are not completely interchangeable, but they often are.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!