Got Questions? Get Answers.
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

Thread Subject:
looping with a function

Subject: looping with a function

From: Jim

Date: 7 Aug, 2010 05:18:05

Message: 1 of 2

Hello guys,

I am coding a rather large application and am faced by another stumbling block and need your help and expertise.

Firstly, as follows is the function:

%======================================
  function Return = Return_answer (x, X)
               Return = -(x * X(1,1) + X(3,1)) / X(2,1);
%======================================

Now in my application I am faced with the multiple values in the 'x' and 'X' variables, hence I'll need to do some looping.

So, I will begin explaining my issue:

      VAL = [ 2 5 6 11;
                       8 7 9 15 ]

xmin = VAL(1)
ymin = VAL(2)
xmax = VAL(3)
ymax = VAL(4)

Now, my first problem here is that with the matrix 'VAL' I want to distinguish 'xmin' , 'ymin' , 'xmax' and 'ymin' for each row in VAL.

I am given in my application 'X' to be defined as:

X = [34 87;
         59 79;
         45 22]

This is where my second problem comes in. In the function 'Return' , X is supposed to be in the form X = [ 34; 59 ;45 ] and X = [87 ; 79; 22].

So, finally , to get my final result, I use the function in code as follows:

%==============================================
X = [ 34; 59 ;45 ];
 VAL = [ 2 5 6 11];

xmin = VAL(1)
ymin = VAL(2)
xmax = VAL(3)
ymax = VAL(4)

Return = Return_answer(xmax, X)
%==============================================

 The return answer is

                               Return = -4.22033898305085

I am new to Matlab and coding in general and not too sure on looping, So basically, I would like to get a return answer using looping.

With the other set of input values into the function as follows:

X = [ 87; 79 ; 22 ];
 VAL = [ 8 7 9 15];

The return answer is

                             Return = -10.1898734177215

To conclude, I want in the end to get a final output answer of the form:

Return =
                           -4.22033898305085
                           -10.1898734177215

If any of you can help, please do, I'm in strife.

jimmy

Subject: looping with a function

From: Sean

Date: 9 Aug, 2010 13:10:25

Message: 2 of 2

"Jim " <iwonder26@gmail.com> wrote in message <i3iqad$fd$1@fred.mathworks.com>...
> Hello guys,
>
> I am coding a rather large application and am faced by another stumbling block and need your help and expertise.
>
> Firstly, as follows is the function:
>
> %======================================
> function Return = Return_answer (x, X)
> Return = -(x * X(1,1) + X(3,1)) / X(2,1);
> %======================================
>
> Now in my application I am faced with the multiple values in the 'x' and 'X' variables, hence I'll need to do some looping.
>
> So, I will begin explaining my issue:
>
> VAL = [ 2 5 6 11;
> 8 7 9 15 ]
>
> xmin = VAL(1)
> ymin = VAL(2)
> xmax = VAL(3)
> ymax = VAL(4)
>
> Now, my first problem here is that with the matrix 'VAL' I want to distinguish 'xmin' , 'ymin' , 'xmax' and 'ymin' for each row in VAL.
>
> I am given in my application 'X' to be defined as:
>
> X = [34 87;
> 59 79;
> 45 22]
>
> This is where my second problem comes in. In the function 'Return' , X is supposed to be in the form X = [ 34; 59 ;45 ] and X = [87 ; 79; 22].
>
> So, finally , to get my final result, I use the function in code as follows:
>
> %==============================================
> X = [ 34; 59 ;45 ];
> VAL = [ 2 5 6 11];
>
> xmin = VAL(1)
> ymin = VAL(2)
> xmax = VAL(3)
> ymax = VAL(4)
>
> Return = Return_answer(xmax, X)
> %==============================================
>
> The return answer is
>
> Return = -4.22033898305085
>
> I am new to Matlab and coding in general and not too sure on looping, So basically, I would like to get a return answer using looping.
>
> With the other set of input values into the function as follows:
>
> X = [ 87; 79 ; 22 ];
> VAL = [ 8 7 9 15];
>
> The return answer is
>
> Return = -10.1898734177215
>
> To conclude, I want in the end to get a final output answer of the form:
>
> Return =
> -4.22033898305085
> -10.1898734177215
>
> If any of you can help, please do, I'm in strife.
>
> jimmy


A couple of hints to look at:
-linear indexing:
ymin = VAL(2) what you expect? what about xmax or ymax?

for whole row indexing:
new_values = matrix(1,:);
for whole column indexing:
new_values = matrix(:,1);

I would recommend doing some of the MATLAB beginner tutorials.

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