Hi eveyone!
Could you please help me on this question?
Or steer me in the right direction?
Write a MATLAB function named add_2() that takes two items
as input and adds them. The function then returns the result of this addition. The two input items could be two scalars, one scalar and one vector, one vector and one scalar, or two vectors. Your function must use one or more conditional statements (if ... end, or if ... else ... end, or if ... elseif ... elseif ... else ... end) to dispatch on the types of input it receives, and, in the case where addition involves one or more vectors, it must use an iteration statement ( for ... end ) to perform itemwise addition. In other words, your function has to behave as though MATLAB's addition operator, +, worked only with scalars and not with vectors and matrices. Your function should also be able to add (itemwise) a row vector with a column vector, as long as they have the same number of items.
Hints: 1) use the function length() to find if an input is a scalar or a vector.
2) to test for equality, use the == operator.
Here are 3 examples of the output that your function should produce:
>> add_2(3,[1 4])
ans =
4
7
>> add_2([1 4 5 7],3)
ans =
4
7
8
10
>> add_2([1 4 5],[4 ; 6 ; 1])
ans =
5
10
4
I'm thinking of using repmat to create the expansion of vectors or scalars (don't think it'll matter at the end of my code if I use an if statement tosee how long the vector/scalar is)
But I'm not sure how to start
I've created items x and y which will be the two inputs then i made m and n which will be the elements for the length(x) and y. Can someone help me in proceeding fromthere?
thankyou!
