Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help on this problem. Thank you! switch on nargin My Solutions > Matlab has a built in summation function, but let's write one of
Need help on this problem. Thank you!
switch on nargin My Solutions > Matlab has a built in summation function, but let's write one of our own. Write a function that 1) will accept one or two inputs, with the first input being a matrix, and the optional second input being a dimension to sum across (row = 1, columns = 2). 2) if only one input was provided, set a default value of the dimension to columns (2). 3) will then sum elements of the matrix along the dimension specified (if dim = 1, then sum along the rows, resulting in a row vector; if dim = 2, then sum along the columns, resulting in a column vector). 4) and will then return this sum to the user. A few ground rules: a) you can't use the built in sum function in Matlab. b) you can use an if() or switch case to change how you do the summation (if dim = 1 or 2). c) you must use a switch case in your code somewhere (hint: use it at least on nargin!). And hints: i) use a for() loop to do the summation. il) if you use a for() loop, be sure to get the size information on X. iil) and if statement will handle the different cases of the dimension really well. Can use an if and else to handle it all. iv) you may want to loop through rows and columns, or columns and rows, of X to add the elements. Function Save e Reset MATLAB Documentation Code to call your function C Reset 1 X = round(rand(3,4)*10) 2 A = mySum(X) 3 B = mySum(X, 1) C = mySum(x, 2)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started