- #1
gfd43tg
Gold Member
- 950
- 50
Homework Statement
The function func takes any real array A as input and outputs an array B of equal size, whose entries are equal to twice the corresponding entry of A for every nonnegative entry of A, and equal to a given number n, for every negative entry of A. The function func accepts a subfunction subf. Complete the code in func and subf, as necessary.
Code:
function B=func(A,n)
% Replace A by B such that each entry of B equals:
% twice the entry of A if that entry of A is non-negative
% or n if the entry of A is negative
B = 2*subf(_______________ % COMPLETE THE ARGUMENT LIST
function C = subf(__________________ % COMPLETE THE
% ARGUMENT LIST
% subfunction of func
C___________________________________ % COMPLETE THE LINE
Homework Equations
The Attempt at a Solution
Code:
function B=func(A,n)
% Replace A by B such that each entry of B equals:
% twice the entry of A if that entry of A is non-negative
% or n if the entry of A is negative
B = 2*subf(A)% COMPLETE THE ARGUMENT LIST
function C = subf(A<0,n) % COMPLETE THE
% ARGUMENT LIST
% subfunction of func
C = n % COMPLETE THE LINE
I am totally confused how to do this. This is so concise that I can't figure out how to make it distinguish whether its negative or non-negative with the few lines of code permitted. I am also having some confusion over how to properly use a subfunction. I would rather use if statements, but since this was an exam question I have to do it the way they ask.