8.2: Computing with Vectors


In this section, we’ll look at two common patterns for working with vectors and connect them to the corresponding ideas from mathematics, existential and universal quantification. And you’ll learn about logical vectors, which contain the Boolean values 0 and 1.

Existential Quantification

It’s often useful to check the elements of a vector to see if there are any that satisfy a condition. For example, you might want to know if there are any positive elements. In mathematical terms, checking whether something exists is called existential quantification, and it’s denoted with the symbol $$\exists$$, which is pronounced “there exists.” For example, $\exists x \mbox{~in~} S: x>0 \notag$ means, “there exists some element $$x$$ in the set $$S$$ such that $$x>0$$.” In MATLAB, it’s natural to express this idea with a logical function, like exists, that returns 1 if there is such an element and 0 if there is not.

function res = exists(X)
for i=1:length(X)
if X(i) > 0
res = 1;
return
end
end
res = 0;
end

We haven’t seen the return statement before ; it’s similar to break except that it breaks out of the whole function, not just the loop. That behavior is what we want here because as soon as we find a positive element, we know the answer (it exists!) and we can end the function immediately without looking at the rest of the elements.

If we get to the end of the loop, that means we didn’t find what we were looking for, so the result is 0.

Universal Quantification

Another common operation on vectors is to check whether all of the elements satisfy a condition, which is called universal quantification, denoted with the symbol $$\forall$$ and pronounced “for all.” So the expression $\forall x \mbox{~in~} S: x>0\notag$ means “for all elements $$x$$ in the set $$S$$, $$x>0$$.”

One way to evaluate this expression in MATLAB is to reduce the problem to existential quantification, that is, to rewrite

$\forall x \mbox{~in~} S: x>0\notag$

to the following: ${\sim} \exists x \mbox{~in~} S: x \le 0\notag$ where $${\sim} \exists$$ means “does not exist.” In other words, checking that all the elements are positive is the same as checking that there are no elements that are nonpositive.

Exercise 8.3

Write a function named forall that takes a vector and returns 1 if all of the elements are positive and 0 if there are any nonpositive elements.

Logical Vectors

When you apply a logical operator to a vector, the result is a logical vector: a vector whose elements are the logical values 1 and 0. Let’s look at an example:

>> V = -3:3

V = -3    -2    -1     0     1     2     3

>> L = V>0

L =  0     0     0     0     1     1     1

In this example, L is a logical vector whose elements correspond to the elements of V. For each positive element of V, the corresponding element of L is 1.

Logical vectors can be used like flags to store the state of a condition. And they are often used with the find function, which takes a logical vector and returns a vector that contains the indices of the elements that are “true.”

Applying find to L from the example above yields

>> find(L)

ans = 5     6     7

which indicates that elements 5, 6, and 7 have the value 1.

If there are no “true” elements, the result is an empty vector.

>> find(V>10)

ans = Empty matrix: 1x0

This example computes the logical vector and passes it as an argument to find without assigning it to an intermediate variable. You can read this version abstractly as “find the indices of elements of V that are greater than 10.”

We can also use find to write exists more concisely:

function res = exists(X)
L = find(X>0)
res = length(L) > 0
end
Exercise 8.4

Write a version of forall using find.

This page titled 8.2: Computing with Vectors is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.