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.

I am pretty new to Mathematica and to coding in general. I want to do a very specific thing, but I unable to do it.

Please explain in detail how I can make this work.

Here's the details on what I am trying to do:

I have the following matrix, which included two variables which are lists in themselves, to analyze these lists using loops I want to make a list of lists using a loop so that the OUTPUT of the function will be this:

matrix = {
  {{0, 1}, {-b3 - gain11[[1]]/m, -a3 - gain22[[1]]/m}},
  {{0, 1}, {-b3 - gain11[[1]]/m, -a3 - gain22[[2]]/m}},
  {{0, 1}, {-b3 - gain11[[1]]/m, -a3 - gain22[[3]]/m}},
  {{0, 1}, {-b3 - gain11[[2]]/m, -a3 - gain22[[1]]/m}},
  {{0, 1}, {-b3 - gain11[[2]]/m, -a3 - gain22[[2]]/m}},
  {{0, 1}, {-b3 - gain11[[2]]/m, -a3 - gain22[[3]]/m}},
  {{0, 1}, {-b3 - gain11[[3]]/m, -a3 - gain22[[1]]/m}},
  {{0, 1}, {-b3 - gain11[[3]]/m, -a3 - gain22[[2]]/m}},
  {{0, 1}, {-b3 - gain11[[3]]/m, -a3 - gain22[[3]]/m}}
}

As you may have noticed, it's the same list repeated 9 times, where it fetches data from two lists, from gain11 and gain22, each with three elements, so that all possible combinations have been made.

I have tried to use

For[i = 1, i < 4, i++,
  For[j = 1, j < 4, j++, {{0, 1}, matrix = {-b3 - gain11[[i]]/m, -a3 - gain22[[j]]/m}}]]

but all that does is overwrite itself. I end up with only the last value.

Help would be appreciated.

share|improve this question
    
Please post a complete working example, with at least some minimum data so readers can better assist. –  rasher Apr 21 at 2:32

2 Answers 2

For-loops are supported in Mathematica so people coming to Mathematica from procedural languages like C and Java will feel comfortable. But For-loops should avoided. They don't fit in well into Mathematica's functional programming paradigm, which means it's tricky to get them right and they produce extremely inefficient code. Better for a beginner to get off to good start by looking into Table, which is Mathematica's high-level function for constructing lists of all kinds. In your case it would go like this.

gain11 = {g111, g112, g113};
gain22 = {g221, g222, g223};
tabl = Table[{{0, 1}, {-b3 - i/m, -a3 - j/m}}, {i, gain11}, {j, gain22}]
{
   {{{0, 1}, {-b3 - g111/m, -a3 - g221/m}}, 
    {{0, 1}, {-b3 - g111/m, -a3 - g222/m}}, 
    {{0, 1}, {-b3 - g111/m, -a3 - g223/m}}}, 
   {{{0, 1}, {-b3 - g112/m, -a3 - g221/m}}, 
    {{0, 1}, {-b3 - g112/m, -a3 - g222/m}}, 
    {{0, 1}, {-b3 - g112/m, -a3 - g223/m}}}, 
   ({{0, 1}, {-b3 - g113/m, -a3 - g221/m}}, 
    {{0, 1}, {-b3 - g113/m, -a3 - g222/m}}, 
    {{0, 1}, {-b3 - g113/m, -a3 - g223/m}}}
}

Now that's almost what you want, except it's a 3 x 3 matrix rather than a list of 9 elements. Not to worry. Mathematica has function to handle this problem which comes up often.

matrix = Flatten[tabl, 1]
{
   {{0, 1}, {-b3 - g111/m, -a3 - g221/m}}, 
   {{0, 1}, {-b3 - g111/m, -a3 - g222/m}}, 
   {{0, 1}, {-b3 - g111/m, -a3 - g223/m}}, 
   {{0, 1}, {-b3 - g112/m, -a3 - g221/m}}, 
   {{0, 1}, {-b3 - g112/m, -a3 - g222/m}}, 
   {{0, 1}, {-b3 - g112/m, -a3 - g223/m}}, 
   {{0, 1}, {-b3 - g113/m, -a3 - g221/m}}, 
   {{0, 1}, {-b3 - g113/m, -a3 - g222/m}}, 
   {{0, 1}, {-b3 - g113/m, -a3 - g223/m}}
}

It can be done, of course, by combining the two steps into one.

matrix = Flatten[Table[{{0, 1}, {-b3 - i/m, -a3 - j/m}}, {i, gain11}, {j, gain22}], 1]

I broke it into two steps so you could see why there is a need to use Flatten

share|improve this answer
    
this is exactly the answer i was looking for, thank you. –  Zimri Leijen Apr 30 at 18:46
Module[{matrix = {}}, 
 For[i = 1, i < 4, i++, For[j = 1, j < 4, j++, 
   AppendTo[ matrix, {{0,1}, {-b3 - gain11[[i]]/m, -a3 - gain22[[j]]/m}}]]]; matrix]

or

Flatten[Array[{{0,1}, {-b3 - gain11[[#1]]/m, -a3 - gain22[[#2]]/m}} &, {3, 3}], 1]

or

{{0, 1}, {-b3 - gain11[[#1]]/m, -a3 - gain22[[#2]]/m}} & @@@ Tuples[Range@3, 2]
share|improve this answer
    
Thanks, I think the above one is the one i was looking for. It seems to do what i need it to do. –  Zimri Leijen Apr 21 at 3:02

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.