3

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.

ChilaDiaz
  • 31
  • 3
  • please have a look here, you may want to change your initialization to dog = ConstantArray[{{1, 1}, {2, 1}, {3, 1}}, 3]. – vapor Jul 25 '17 at 02:37
  • @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. – ChilaDiaz Jul 25 '17 at 02:59
  • 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. – Felix Jul 25 '17 at 03:41
  • 1
    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. – faysou Jul 25 '17 at 06:58
  • @faysou Agreed; I just posted an answer below with that. – Mr.Wizard Jul 25 '17 at 07:00
  • Thank you very much, @Felix. You were right, I had to use double square brackets, I felt so dumb when I tried it after approaching in several different ways without success. And also thanks to everyone of you who helped. – ChilaDiaz Jul 25 '17 at 08:29

2 Answers2

4

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}}}

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
3

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]].

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371