check if all entries of a vector are either true or false in matlab

You can use the all function to check if all entries of a logical vector are either true or false.

For example:

main.m
v = [true, false, true];
result = all(v); % result is false because not all entries are either true or false

v = [true, true, true];
result = all(v); % result is true because all entries are true

v = [false, false, false];
result = all(v); % result is true because all entries are false

290 chars
10 lines

If your vector is not already a logical vector, you can convert it using the logical function.

For example:

main.m
v = [1, 0, 1];
result = all(logical(v)); % result is false because not all entries are either true or false

v = [1, 1, 1];
result = all(logical(v)); % result is true because all entries are true

v = [0, 0, 0];
result = all(logical(v)); % result is true because all entries are false

286 chars
10 lines

related categories

gistlibby LogSnag