3
$\begingroup$

I'm new to Mathematica and I'm trying to work with Do for a set of indexed objects in order to avoid doing the same action repeatedly. For example, I create the following lists:

dog[1] = {{1, 1}, {5, 7}, {3, 10}}; 
dog[2] = {{7, 2}, {11, 8}, {4, 3}};
dog[3] = {{1, 7}, {1, 5}, {9, 3}};

And I want to change the value of the second columns on each of these 10 times. My approach to the problem:

Do[Part[dog[i], All, 2] = 10*Part[Perro[i], All, 2], {i, 3}]

But it returns me the error: dog[i] in the part assignment is not a symbol. How can this be fixed? I would really appreciate your help.

$\endgroup$
7
  • $\begingroup$ please have a look here, you may want to change your initialization to dog = ConstantArray[{{1, 1}, {2, 1}, {3, 1}}, 3]. $\endgroup$
    – vapor
    Commented Jul 25, 2017 at 2:37
  • $\begingroup$ @happyfish Thanks for your advice, but my example was wrong. I'm now working with lists that have the same number in their columns, I wrote it that way just to give and example that's why I edited it. $\endgroup$
    – ChilaDiaz
    Commented Jul 25, 2017 at 2:59
  • $\begingroup$ Is dog a function? It looks to me that you want dog to be a vector. To access vector components, you need double square brackets [[]]. If it's a function, I don't think you can assign to a part of it. $\endgroup$
    – Felix
    Commented Jul 25, 2017 at 3:41
  • $\begingroup$ Related: (72624), (148387) $\endgroup$
    – Mr.Wizard
    Commented Jul 25, 2017 at 6:45
  • 1
    $\begingroup$ You would be better using an Association if you have Mathematica 10 or later, you wouldn't have any problem doing what you want with it. $\endgroup$
    – faysou
    Commented Jul 25, 2017 at 6:58

2 Answers 2

4
$\begingroup$

I do the kind of thing you describe quite frequently and to do it I use what I call the Module trick. Part needs a variable as its 1st argument when it is used on the lefthand side of an assignment. The idea is use a local variable in a module. Here is an example using your data.

dog[1] = {{1, 1}, {5, 7}, {3, 10}};
dog[2] = {{7, 2}, {11, 8}, {4, 3}};
dog[3] = {{1, 7}, {1, 5}, {9, 3}};

Array[dog, 3]

{{{1, 1}, {5, 7}, {3, 10}}, {{7, 2}, {11, 8}, {4, 3}}, {{1, 7}, {1, 5}, {9, 3}}}

Do[dog[i] = Module[{d = dog[i]}, d[[All, 2]] = 10 d[[All, 2]]; d], {i, 3}]

Then

Array[dog, 3]

{{{1, 10}, {5, 70}, {3, 100}}, {{7, 20}, {11, 80}, {4, 30}}, {{1, 70}, {1, 50}, {9, 30}}}

$\endgroup$
3
$\begingroup$

Since version 10 Associations provide a new approach:

dog = <||>;

dog[1] = {{1, 1}, {5, 7}, {3, 10}};
dog[2] = {{7, 2}, {11, 8}, {4, 3}};
dog[3] = {{1, 7}, {1, 5}, {9, 3}};

Do[dog[[i, All, 2]] *= 10 i, {i, 3}]

dog
<|1 -> {{1, 10}, {5, 70}, {3, 100}},
  2 -> {{7, 40}, {11, 160}, {4, 60}},
  3 -> {{1, 210}, {1, 150}, {9, 90}}|>

This works also with arbitrary key names like "a", "b", "c", which can be addressed with either the integer position or the key name itself: dog[[1, All, 2]] or dog[["a", All, 2]].

$\endgroup$

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