Hi all.

~~As every wee~~k*, we will have a tip for those who want to vectorize their matlab programs, for reasons already stated in earlier posts. In this case, we will look at the vectorization of piecewise functions.

Let’s say you have a function like this :

which, when plotted, would look like this

OK. So, what’s the deal? Well, from the earlier posts on vectorization, you may have seen that we always did simple stuff. If you have coded a piecewise function before in Matlab, you know that it’s something like this

function y = pieceWise(x) if(x>a1 && xa2 && x y = a different value here . . . else y = a value in the case all else fails! end

So, this code would implement a piecewise function which changes in the intervals . For our own example, the code would be

function y = piecewise2(x) if(0<=x && x<1/2) y = x; elseif(1/2<=x && x<1) y = x.^2; else y = 0; end end

where we supposed that our function is zero outside of [0,1]. Our goal is to write that function in a format where if x is a vector then y = f(x), another vector.

Of course, trying that with the way our function is written will not work. Give that function a vector as input and you will get back

??? Operands to the || and && operators must be convertible to logical scalar values. Error in ==> piecewise2 at 2 if(0<=x && x<1/2)

That’s because the && operator is not built to handle** vectors of logical expressions**.

Wait, what? … What’s a vector of logical expressions in Matlab?

Well, think of it this way. When we have a logical expression like x>0, then this expression can give back two possible numbers. Feed it with a positive x and it gives back 1. Else it would just return 0. But what would happen if we changed the scalar x with a vector of real numbers? Isn’t it logical to expect another vector with 0s and 1s, according to which elements of are greater (or smaller) than 0?

Either way, that’s what happens. If , then .

So, here’s a question for extra credit. Say we have and . Then, what does the piecewise product of the two vectors, and stand for? I will wait for a little bit, so you can figure this out.

. . . . .

If you’ve thought about it, you found out that is nothing else than the vector of the conditional expression that would be generated by (if that were possible).

So how do we use this idea? We can’t just go in our code and change it to

function y = piecewise2(x) if((0<=x).*(x<1/2)) y = x; elseif((1/2<=x).*(x<1)) y = x.^2; else y = 0; end end

The problem here is that the if-clause is not gonna figure it out that you want to do a separate “if” for every element of the vector . It’s gonna only return the first value that it finds. Try it and see for yourselves.

So, how can we use this information? We only need to think this function from a different perspective.

Isn’t this function

equal to this

???

And isn’t the conditional expression just ? Note that is called the characteristic function of set A and it equals 1 if x belongs in that set, 0 if not.

Using all of those ideas, we can write our function as

function y = piecewise2(x) y = ((0<=x).*(x<1/2)).*x + ((1/2<=x).*(x<1)).*x.^2;

Congrats! This function is now vectorized! With the same idea, you can vectorize complex conditionals statements by just thinking about the arithmetic between them. Assign values of 1 and 0 to the true and false statements and think how you could gain the same results.

Try to figure out how would work by doing arithmetic between those vectors.

Until next time.

*Well, I am not very consistent with that “every week” promise. I should probably rename these posts as “Vect tip of the month”. 😛