Quick matlab question regarding if statements/for loops - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Quick matlab question regarding if statements/for loops

Posts: 2Member
I need to write a code for if x>a then calculate (x-a)^n, if x<=a output 0. I figure my initial issues were that since x can be scalar OR VECTOR then i had to adapt the code for that. I am having issues picking each element from vector x.

My code so far
function y=sfun(x,a,n)
x=input('enter "x" as a vector [a b] or variable: ');
n=input('enter "n" parameter: ');
a=input('enter "a" parameter: ');
k=length(x);
for z=x(1:k)
if z>a
y=(z-a).^n;
else
y=0;
end
end

I get 27. Which is just the correct answer for 5. When x=0 it should be giving me 0 obviously which it is not.

Added attachment of the piecewise function I am trying to make.