# Apply if/else if to each element in array in MATLAB

oceanspiral20
Hi!
I have an array that is 1 column by many rows, of which each element represents a time. I want to find variable f at each time, where f is defined as follows:

if t < tp
f=(sin((pi*t)/(2*tp)))^2;
if tp < t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
otherwise f=0

I've tried defining f as above in a .m function file (called elastance), and then using f=arrayfun(elastance,t) to find f at each time, but that doesn't work (It says, "Error in ==> elastance at 5, if t < tp".) Just typing the above function directly into the command window doesn't work either, whether or not I put it in terms of t or (t.).

Does anyone have any suggestions? Thanks!

## Answers and Replies

Mentor
Hi!
I have an array that is 1 column by many rows, of which each element represents a time. I want to find variable f at each time, where f is defined as follows:

if t < tp
f=(sin((pi*t)/(2*tp)))^2;
if tp < t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
otherwise f=0

I've tried defining f as above in a .m function file (called elastance), and then using f=arrayfun(elastance,t) to find f at each time, but that doesn't work (It says, "Error in ==> elastance at 5, if t < tp".) Just typing the above function directly into the command window doesn't work either, whether or not I put it in terms of t or (t.).

Does anyone have any suggestions? Thanks!
I see several things that might be causing your problems:
othewise is used in switch statements, not in if - elseif - else statements.
Your 2nd if statement was testing a malformed expression (i.e., tp < t < ts). In most programming languages this should be made into two expressions.

The changes below might fix your problems.

Code:
if t < tp
f=(sin((pi*t)/(2*tp)))^2;
elseif if tp < t & t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
else f=0
end

oceanspiral20

I did fix those errors, but still can't get it to work and apply to each element in the array. I've done it both just entering this code into the command window, and it only returns one element f=0 instead of f values for each time step in the array... Here's the code I'm using:

if t < tp
f=(sin((pi*t)/(2*tp)))^2;
elseif tp < t & t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
else f=0
end

Also tried making a separate function with it and using arrayfun, as follows:

function f=elastance2(t)
tp=.35;
ts=.8;

if t < tp
f=(sin((pi*t)/(2*tp)))^2;
elseif tp < t && t < ts
f=(cos((pi*(t-tp))/(2*(ts-tp))))^2;
else f=0;
end

and in the command window:
f=arrayfun(elastance2,t)

but I get the following error:

??? Input argument "t" is undefined.

Error in ==> elastance2 at 5
if t < tp

t is the array of times; if I type t and enter the array comes up, so I'm not sure why it is undefined. Does someone have any advice?

neurocomp2003
your function works fine when i plug it in but you seem to have some errors?....is f intended to be an output array corresponding to your functions?

you need to use the . operator for array manipulation and the zeros and size functions for your else condition...

did you try defining t inside to see if the function works....or the stupid quesiton is if you defined it before calling the function. try using a different letter encase yoru using two different letters.

Ben H
Hi oceanspiral20,
I will define an array for time t, which will be many columns in one row, the opposite to what you said but this is pretty standard. Then using a for loop, we look at each t and check what conditions it satisfies etc. I have had to make up some constants which should be similar to yours. Here's the code:
%________________________________________________________
clear all
t = 0:0.01:0.2;
tp = 0.09;
ts = 0.15;
for h = 1:length(t)
if t(h) <= tp
f(h)=(sin((pi*t(h))/(2*tp)))^2;
elseif t(h) > tp & t(h) <= ts
f(h)=(cos((pi*(t(h)-tp))/(2*(ts-tp))))^2;
else
f(h)=0;
end
end
%_____________________________________________________

I have set tp as less than ts as is evident from your post. The time range is 0 to 0.2 seconds. So, when t is less than tp, it applies the first expression involving sin, when tp < t < ts, it applies the second expression invoving cos and when t is greater than ts (this addresses your 'otherwise' condition) it sets f = 0. Note the use of (h) to indicate looking at individual array elements.
Just set your values for t, tp and ts and it should work fine.

This code should do what you want when used independently, however if you are using it within another script you may need to make some adjusments. In other words, this doesn't need any defined functions to work.

Last edited:
oceanspiral20
Hi Ben H!

Thank you so much for your reply, that worked beautifully! :)