# MATLAB - Function M-Files

• Mar 26th 2010, 01:08 PM
Kevlar
MATLAB - Function M-Files
Hi guys i'm new to this software package and require some assistance on making 2 function M-files...

1)Write a function file replace that replaces all elements in a row vector that are between -2 and +2 by 0. The function file should operate on row vectors of arbitrary length. You are not allowed to use the Matlab function find.
Hints: Use a for loop and an if statement. The length of a row vector x can be
obtained using the Matlab command length(x).

2) Write a function file FindPositive that takes in a row vector of arbitrary length and returns the first positive value in the row vector, as well as the index of the row vector where the first positive value occurred. You are not allowed to use a for loop or an if statement in your solution.
• Mar 26th 2010, 03:36 PM
CaptainBlack
Quote:

Originally Posted by Kevlar
Hi guys i'm new to this software package and require some assistance on making 2 function M-files...

1)Write a function file replace that replaces all elements in a row vector that are between -2 and +2 by 0. The function file should operate on row vectors of arbitrary length. You are not allowed to use the Matlab function find.
Hints: Use a for loop and an if statement. The length of a row vector x can be
obtained using the Matlab command length(x).

Obeying the rule that find not be used, but not using the hint and probably getting no marks:

Code:

```function rv=repl(x)   rv=x-((x<2)&&(x>-2)).*x;```
• Mar 26th 2010, 03:47 PM
CaptainBlack
Quote:

Originally Posted by Kevlar
Hi guys i'm new to this software package and require some assistance on making 2 function M-files...

1)Write a function file replace that replaces all elements in a row vector that are between -2 and +2 by 0. The function file should operate on row vectors of arbitrary length. You are not allowed to use the Matlab function find.
Hints: Use a for loop and an if statement. The length of a row vector x can be
obtained using the Matlab command length(x).

2) Write a function file FindPositive that takes in a row vector of arbitrary length and returns the first positive value in the row vector, as well as the index of the row vector where the first positive value occurred. You are not allowed to use a for loop or an if statement in your solution.

The second is easy, using the hint of what you were not allowed to use in part 1 (use find). That is unless you have to guard against there being no positive values?

CB
• Mar 26th 2010, 04:20 PM
Kevlar
for the 1st one i was doing somethin along the lines of

Code:

```function b=replace(a) %This function replaces all elements in a row or column that are %between 2 and -2 with 0 and assisns the new elements to the row b L=length(a);   for k=1:L       if a(k)<2|a(k)>-2         b(k,1)=0;       else         b(k,1)=a(k); end end```
(Can't check my code till monday now don't have matlab on my pc only on the uni pc's)

For the second one still not sure as i dunno how to use the find function as of yet >.< !
• Mar 26th 2010, 11:50 PM
CaptainBlack
Quote:

Originally Posted by Kevlar
for the 1st one i was doing somethin along the lines of

Code:

```function b=replace(a) %This function replaces all elements in a row or column that are %between 2 and -2 with 0 and assisns the new elements to the row b L=length(a);   for k=1:L       if a(k)<2|a(k)>-2         b(k,1)=0;       else         b(k,1)=a(k); end end```
(Can't check my code till monday now don't have matlab on my pc only on the uni pc's)

For the second one still not sure as i dunno how to use the find function as of yet >.< !

You can install FreeMat for basic matlab coding jobs, its free and pretty good.

The or in your code should be an and (and last time I looked the editor wanted me to use || and && rather than | and &, but both seem to still work)

CB
• Mar 27th 2010, 05:02 AM
elbarto
Second to CB's post, Octave is also pretty good (and the syntax is ~95% transparent between languages I believe). If you are going to be using matlab for an extended period of time however, the student version is great value for money.

Elbarto
• Mar 27th 2010, 11:20 PM
CaptainBlack
Quote:

Originally Posted by elbarto
Second to CB's post, Octave is also pretty good (and the syntax is ~95% transparent between languages I believe). If you are going to be using matlab for an extended period of time however, the student version is great value for money.

Elbarto

The student version is indeed great value for money, and this is one of the things I dislike about MathWorks.

A complete distribution is available to students to turn them into Matlab users for life, so when they get jobs they will want their employers to provide Matlab as their tool of choice. Now here is the rub, the Matlab installation on my work machine is by no means complete and it has cost my employer about £10000 to provide + annual maintenance fees (there is a basic price for the Matlab core functionality then ca £600 per additional toolbox and you need a fair number of those tool boxes).

Educational institutions should not be aiding in this conspiracy.

Employers with a corporate policy of not using open source software (yes, I'm looking at you RandomTLA-Systems) are also to blame for this situation (assuming their accounting systems give them any indication of how much they are paying MathWorks for those 1000's of Matlab seats)

(I won't mention the ludicrous amount of time it takes Matlab to boot, when using an alternative I will have solved the kind of small problem that often comes up before Matlab will have booted)

CB