Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

My homework

Name: Anonymous 2006-11-25 12:22

i'm desperate, i have no clue how to programm
if anyone can make my homework i'd largely appreciate it cuz i have to deliver it a few hours

It's to be done in Mathematica, which i've been told is a lot like C

"Using only the list functions First, Last, Rest, Append, Prepend, Length, {}, [[.]] and [[.,.]], define in Mathematica an 'f' function that receives as an argument a square matrix of natural numbers (including 0), and goes through it using two boxed (one inside the other) While cycles and returns the list of lines where the minimum of the matrix occurs (without repetitions)."

Name: Anonymous 2006-11-25 17:44

>>1

Ok, to define a function f that takes a single parameter (your matrix, notated by 'm'), you need to do it like so, putting the content of your function in between the brackets:

f[m_] := (

)

Multiple instrucions you put in your function need to be separated by semicolons.

A matrix is defined as a list of lists. A list is notated as curly brackets with its elements separated by commas. So, for example, here is a matrix and its corresponding list definition:

matrix m

/     \
| 1 2 | 
| 3 4 |
\     /

in mathematica,

m = { {1, 2}, {3, 4} }

This is the purpose of the {} function mentioned in your question. The [[.]] and [[.,.]] functions operate on the matrix - the former returning a row and the latter an individual element.

Example, using the previous matrix:

m[[1]] asks for row 1, which is {1, 2}
m[[2,1]] asks for the 1st element of row 2, which is 3

The First and Last functions are straightforward too, getting the first and last elements of a list. So if you had a list L = {1, 2, 8, 4} then First[L] is 1 and Last[L] is 4.

Rest[L] is interesting in that it deletes the first element of the list and just gives you the rest of it - the previous list would be {2, 8, 4} when this function is applied. You can use this to help you iterate through the elements of a list.

Prepend and Append functions add an element to the start and end of the list respectively. So using the list L above,

Prepend[L, 5] gives you {5, 1, 2, 8, 4}
Append[L, 5] gives you {1, 2, 8, 4, 5}

The Length function just gives you the length of a list. In your case, this will be useful for checking for an empty list (i.e. Length returns 0)

Finally, the While function is what will tie all this together. It's not hard to use when you get your head round it. Basically what it does is evaluates the first parameter - if that is true then it evaluates the second. It repeats this until the first parameter evaluates to false.

Here's an example:

L = {1, 2, 8, 4}
While[
  Length[L] > 0,
  Print[L[1]];
  L = Rest[L]
]

This prints out all the elements in the list L.

Hope all this explanation helps you with your homework. Good luck, Anonymous!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List