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

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Function
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB programming problem where participants are tasked with modifying array elements conditionally using a subfunction. The goal is to create a function that outputs an array based on the conditions of the input array, specifically handling nonnegative and negative entries differently. The conversation includes aspects of programming logic, array manipulation, and the use of subfunctions in MATLAB.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants express confusion over how to implement the required functionality within the constraints of the provided code structure.
  • There is a suggestion that the subfunction may not need to exist, as MATLAB can handle conditional checks directly on arrays, which some participants find redundant.
  • Participants discuss the placement of the multiplication factor of 2 and how it affects the output, with some suggesting it may be incorrectly positioned in the proposed solution.
  • Some participants note that the question appears cryptic and lacks clarity, making it difficult to determine the intended use of the subfunction.
  • There is a mention of the possibility that the question is part of a computer-graded exam, which may require a specific "correct" answer for grading purposes.
  • A participant references a previous exam question that indicates a pattern in the instructor's preference for subfunction problems, suggesting that this might be a recurring theme in assessments.

Areas of Agreement / Disagreement

Participants generally agree that the question is unclear and that the use of a subfunction may not be necessary. However, there is no consensus on how to best implement the solution, and multiple competing views remain regarding the structure and logic of the code.

Contextual Notes

Some participants highlight limitations in the problem statement, including missing details about the expected behavior of the subfunction and how to handle negative entries effectively. There is also uncertainty regarding the grading criteria for the exam question.

gfd43tg
Gold Member
Messages
949
Reaction score
48

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
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"?
 
Sorry I forgot to mention it's Matlab
 
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".
 
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:
 
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.
 
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.
 
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
 

Similar threads

Replies
43
Views
5K
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
Replies
9
Views
2K