How Can You Modify Array Elements Conditionally Using a Subfunction in MATLAB?

  • Thread starter gfd43tg
  • Start date
  • Tags
    Function
In summary, 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.
  • #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.
 
Physics news on Phys.org
  • #2
Apparently your programming language has some methods of array manipulation without looping over the events. It would be helpful to know which language this is and how you can work with arrays.

B = 2*subf(A)% COMPLETE THE ARGUMENT LIST
That way, subf does not know about n, and your B cannot depend on n any more. This cannot work.
B = 2*subf(A)% COMPLETE THE ARGUMENT LIST
function C = subf(A<0,n) % COMPLETE THE
Does your subf have one or two arguments? The lines are contradicting.

"function" mainly does the multiplication with 2. Ignore that for a moment (it's there already anyway).
At some point you have to check which elements of A are negative. "function" does not do it, so "subf" has to do it. What's left to do for "function"?
 
  • #3
Sorry I forgot to mention it's Matlab
 
  • #4
I agree the question seems far too cryptic without some more information about what you are supposed to do.

You could compute the required result with a single assignment statement, so it's not obvious (to me) what the subfunction is for.

"B = 2 * subf(..." seems to be putting the 2 in the wrong place, unless the answer is supposed to be something like "B = 2*subf(...) + another_expression".
 
  • #5
mfb said:
At some point you have to check which elements of A are negative. "function" does not do it, so "subf" has to do it. What's left to do for "function"?

Well, in Matlab the expression "A < 0" gives you an array populated with 1s for the negative terms in A and 0's otherwise (the OP has used that in the attempt at a solution) so it's hardly a reason for writing a subfunction. :confused:
 
  • #6
AlephZero said:
Well, in Matlab the expression "A < 0" gives you an array populated with 1s for the negative terms in A and 0's otherwise (the OP has used that in the attempt at a solution) so it's hardly a reason for writing a subfunction. :confused:
Yeah, but for some reason we have to use one.

The 2 is at an odd place, but we can divide n by 2 to fix that.

It is a weird question, but I think there are solutions.
 
  • #7
Sure there are plenty of solutions, but from the OP's other threads these examples seem to be computer-graded, so you probably have to guess the "right" solution is to get marks.

Maybe there is some more information about the question that the OP hasn't shown us.
 
  • #8
This is a problem from a previous exam. The exam is done by hand, I'll send a picture
ImageUploadedByPhysics Forums1407111097.514119.jpg


Here is an exam question from a different year. It's clear that the instructor likes sub function problems on the exam

ImageUploadedByPhysics Forums1407111166.817324.jpg
 

FAQ: How Can You Modify Array Elements Conditionally Using a Subfunction in MATLAB?

What is a subfunction?

A subfunction is a smaller or more specific function that is nested within a larger function. It is used to break down complex tasks into smaller, more manageable steps.

How do you create a subfunction?

To create a subfunction, you can use the "def" keyword in most programming languages, followed by the name of the subfunction and a set of parentheses. Within the parentheses, you can specify any parameters that the subfunction will take in. Then, indent the code for the subfunction below the "def" statement.

What is the purpose of using subfunctions?

Subfunctions are useful for organizing and modularizing code. They allow you to break down a larger task into smaller, more manageable steps, making it easier to read and maintain. Subfunctions can also be reused multiple times within a larger function or in other parts of the code.

Can a subfunction call another subfunction?

Yes, a subfunction can call another subfunction. This is known as function nesting. It allows you to break down a task into even smaller steps and can help with code organization and readability.

Are there any limitations to using subfunctions?

There are generally no limitations to using subfunctions, but it is important to consider the performance impact of function nesting. If you have too many levels of nested subfunctions, it may affect the speed and efficiency of your code. It is also important to properly scope variables within subfunctions to avoid any unexpected errors.

Back
Top