Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Does anyone know how I can do this in Mathematica? Just an example..

Defining indices and variables (defined over indices), "mapping" variables and indices:

index i {NewYork,LosAngeles,Chicago}
Subindex j {NewYork,LosAngeles}
Index k {a,b,c}

variable costs = costs(i,k)
variable size = size(i)
variable biggest = biggest (j,k)

After that, I can start my calculations..

costs(i,k)=...

What about the notation package? Does it help?

Thanks!!

share|improve this question
    
Welcome to Mathematica.StackExchange. I find this question rather ambiguous as written. Please provide precise examples of input and output behavior that you expect. –  Mr.Wizard Jun 12 '12 at 12:44
    
Sorry, it's a beginner's question. I'm just looking for a way to organise my file. I could define lists, like i{NewYork,LosAngeles,Chicago}. But how can I define that variable costs, eg, depend on i: costs(i), so that I can use costs(i) in my calculations, like: costs(i)=10*x... –  Stefanie Schubert Jun 12 '12 at 13:33
    
@StefanieSchubert From your comment it seems you're looking for function definitions as well. You might start looking in this part of the documentation. Better yet, please read at least the first two chapters in The Virtual Book on Mathematica. –  Sjoerd C. de Vries Jun 12 '12 at 14:53
add comment

1 Answer

up vote 8 down vote accepted

Unlike arrays in many other languages, in many cases Mathematica allows you to deal with lists of data without the need for indexes at all. Lists can be of variable depth and lengths if you need them to be.

cities = {"NewYork","LosAngeles","Chicago"};
costs  = {{1,2}, {3,4},{5,6}};
Transpose[{cities, costs}]

This gives you a list of cities and associated information, in this case costs.

{{NewYork, {1, 2}}, {LosAngeles, {3, 4}}, {Chicago, {5, 6}}}

You could then extract the biggest cost for each city by something along these lines:

{First@#, Max@Last@#} & /@ Transpose[{cities, costs}]

{{NewYork, 2}, {LosAngeles, 4}, {Chicago, 6}}

which works by applying the unnamed function {First@#, Max@Last@#} & over the list of city cost using the Map function, which here is written as /@. The element # here stands for the function argument, which Map replaces with each element, in turn, of the list it is applied to.

This is a common programming pattern in Mathematica and can be used to great effect to achieve many tasks that would require a loop construct in other languages.

share|improve this answer
    
i'll work on that.. thanks a lot! –  Stefanie Schubert Jun 13 '12 at 8:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.