~A returns
a logical array of the same size as A. The array
contains logical 1 (true) values
where A is zero and logical 0 (false)
values where A is nonzero.
not(A) is
an alternate way to execute ~A, but is rarely used.
It enables operator overloading for classes.
Execute code based on a condition using the logical not operator in the context of an if loop.
Create a logical variable A.
A = false;
Use A to write an if/else code block. Wrap the if/else block in a for loop so that it executes four times.
for k = 1:4
if ~A
disp('IF block')
A = true;
else
disp('ELSE block')
endend
IF block
ELSE block
ELSE block
ELSE block
On the first iteration, A is false, so the if block executes since ~A is true. However, the if block also changes the value of A to true. In the remaining iterations, ~A is false and the else block executes.
This function fully supports tall arrays. For
more information, see Tall Arrays.
Tips
You also can use the ~ symbol as
a placeholder output argument in a function call. For example, [~,i]
= max(A) suppresses the first output of the max function,
returning only the indices of the maximum values. For more information,
see Symbol Reference.