Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
Adding two elements of different dimensions

Subject: Adding two elements of different dimensions

From: lclc kindle

Date: 25 Oct, 2010 03:03:03

Message: 1 of 1

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 =
    >> add_2([1 4 5 7],3)
    ans =
    >> add_2([1 4 5],[4 ; 6 ; -1])
    ans =

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?


Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us