1
$\begingroup$

I'm still wrapping my head around Mathematica syntax. I have a matrix clustersAll that looks like

{{{1, 4, 13, 22, 28, 35, 48, 58}, {2, 8, 11, 19, 20, 25, 26, 29, 30, 
   31, 32, 33, 36, 37, 41, 46, 47, 51, 52, 61, 63, 65}, {3}, {5, 16, 
   21, 23, 34, 39, 40, 45, 53, 55, 56, 60, 62, 64, 66}, {6, 9, 10, 50,
    54, 57, 68}, {7, 12, 17, 24, 27, 44, 49}, {14, 42, 43}, {15, 18, 
   67}, {38, 59}}, 
 {{1, 7, 8, 19, 25, 26, 28, 29, 32, 36, 38, 44, 46, 
   61}, {2, 6, 10, 21, 23, 30, 34, 40, 42, 43, 45, 60, 64, 
   65}, {3}, {4, 5, 18, 20, 31, 33, 35, 41, 47, 51, 52, 67}, {9, 12, 
   13, 15, 22, 24, 48}, {11, 37}, {14, 27, 58, 59}, {16, 17, 49}, {39,
    50, 53, 55, 56, 62, 63}, {54, 68}, {57, 66}}, 
 {{1, 2, 3, 4, 5, 7, 
   8, 10, 11, 18, 19, 20, 25, 26, 28, 30, 31, 33, 34, 41, 47, 52, 
   61}, {6, 12, 13, 21, 22, 24, 29, 32, 35, 36, 46, 48, 51, 58, 
   64}, {9, 16, 27, 50, 55, 66}, {14, 38, 44, 59}, {15, 17, 43}, {23, 
   37, 39, 40, 45, 49, 53, 60, 63, 65}, {42, 54}, {56, 67}, {57, 62, 
   68}}, 
 {{1, 49, 59}, {2, 5, 8, 16, 23, 27, 34, 39, 43, 53, 55, 56, 
   62, 66}, {3, 32, 37}, {4, 6, 7, 11, 12, 13, 54, 57, 68}, {9, 15, 
   17, 20, 21, 30, 31, 33, 35, 38, 40, 41, 42, 45, 48, 51, 52, 58, 60,
    64, 65}, {10, 14, 24, 50}, {18, 19, 25, 26, 28, 29, 36, 44, 46, 
   47, 61, 63}, {22, 67}}
 ...}

Each 2nd-level list represents a state and holds lists representing clusters. The numbers in each cluster are 3rd-level indexes from xyPairsAll, or cities.

xyPairsAll is a different matrix that looks like

{...
{{{1., 1812.}, {2., 10076.}, {3., 4764}},   
 {{1., 3475.}, {2., 3572.}, {3., 3985.}}}, 
{{{1., 6839.}, {2., 3849.}, {3., 2746}},    
 {{1., 3578.}, {2., 5629.}, {3., 3849.}}}, 
{{{1., 6839.}, {2., 3849.}, {3., 2746}},    
 {{1., 10092.}, {2., 1638.}, {3., 3728.}}}
...}

where each 2nd-level list represents a state (e.g. xyPairsAll[[1]] is the first state), and each 3rd-level list represents a city and contain {x, y} pairs.

I have a function calcMean that takes in one state and one cluster like calcMean[state_, cluster_] and calculates the mean of y-values across cities and returns an {x, y} pair like {{1., 3137.63}, {2., 10810.9}, {3., 7605.25}}.

To find the mean for each cluster in the first state: calcMean[xyPairsAll[[1]], #] & /@ clustersAll[[1, All]]

How do I take this statement and make a loop over the remaining states, along with iterating through each sublist in clustersAll? The next state would be like this: calcMean[xyPairsAll[[2]], #] & /@ clustersAll[[2, All]]

$\endgroup$
  • 2
    $\begingroup$ maybe Table[calcMean[xyPairsAll[[i]], #] & /@ clustersAll[[i]], {i, Length@clustersAll}]? $\endgroup$ – kglr Jul 9 '18 at 3:23
  • $\begingroup$ It seems to work! Thanks, am new to Mathematica, sometimes it's difficult for me to figure out which method I need @kglr $\endgroup$ – briennakh Jul 9 '18 at 3:38
  • 1
    $\begingroup$ @briennakh Just some advice: You make yourself mad by creating such nested and ragged lists. 1. You get performance degradations when using large data sets. It is a better strategy to store data in so-called packed arrays. For example, instead of storing the coordinates(?) of cities in some subarray, you can store them in a separate list cities of coordinates. The i-th city can than simple be accessed with cities[[i]]. You can map a function f over serveral of such datasets with MapThread[f, {cities, ...}]. $\endgroup$ – Henrik Schumacher Jul 9 '18 at 7:29
  • 1
    $\begingroup$ 2. and probably more important to you: As you experience yourself, such arrays are hard to maintain. Have a look at Association. It allows you to organize your data with human readable keys, e.g. data = Associtation[ "cities" -> { ... }, ...]. You can extract the cities with data[["cities"]]. This removes the need of counting the positions within a dataset and is more readable. Plus, this representation does not depend on the ordering of the constitutents. $\endgroup$ – Henrik Schumacher Jul 9 '18 at 7:32
  • $\begingroup$ @HenrikSchumacher thank you! $\endgroup$ – briennakh Jul 9 '18 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.