Skip to main content
Tweeted twitter.com/StackMma/status/855589332291334144
added 184 characters in body
Source Link
Lokdal
  • 405
  • 2
  • 8

I have a list dimensions {4, 2, ?, 3}. The ? can be any value from 1 to 5. Here's a simplified example:

data = {{{{-1,0.2,0}},{{-1,0.5,0}}},
{{{1,0.47,0},{-1,0.02,0},{1,1.0,0}},{{-1,0.12,0},{-1,0.11,0},{1,0.,0}}},
{{{1,0.1,0},{1,0.41,0},{-1,0.32,0}},{{1,0.02,0},{1,0.33,0},{1,0.01,0}}},
{{{-1,0.1,0},{-1,0.1,0}},{{1,0.41,0},{-1,0.6,0}}}};

I need to calculate an average progressively on the second value of the last dimension, which I've calculated this way so far:

second = Flatten[data[[All,All,All,2]]];
mean = Accumulate[second]/Range[Length[second]];

Now, I wish to replace the old values in data with this calculated average :

result = {{{{-1,0.2,0}},{{-1,0.35,0}}},
{{{1,0.39,0},{-1,0.2975,0},{1,0.438,0}},{{-1,0.385,0},{-1,0.345714,0},{1,0.3025,0}}},
{{{1,0.28,0},{1,0.293,0},{-1,0.295455,0}},{{1,0.2725,0},{1,0.276923,0},{1,0.257857,0}}},
{{{-1,0.247333,0},{-1,0.238125,0}},{{1,0.248235,0},{-1,0.267778,0}}}};

If the dimensions were known, it wouldn't be a problem. But they aren't. Also, my real dimensions are along the lines of {40, 10000, ?, 3}. I will be running this transformation thousands of time, so speed is key.

Any helpEDIT : This is appreciated =)what I've tried and it works, but I'm certain there is a better way

count = 0;
data[[All, All, All, 2]] = Table[++count;
mean[[count]], {n, 4}, {t, 2}, {l, Length[data[[n, t]]]}]

I have a list dimensions {4, 2, ?, 3}. The ? can be any value from 1 to 5. Here's a simplified example:

data = {{{{-1,0.2,0}},{{-1,0.5,0}}},
{{{1,0.47,0},{-1,0.02,0},{1,1.0,0}},{{-1,0.12,0},{-1,0.11,0},{1,0.,0}}},
{{{1,0.1,0},{1,0.41,0},{-1,0.32,0}},{{1,0.02,0},{1,0.33,0},{1,0.01,0}}},
{{{-1,0.1,0},{-1,0.1,0}},{{1,0.41,0},{-1,0.6,0}}}};

I need to calculate an average progressively on the second value of the last dimension, which I've calculated this way so far:

second = Flatten[data[[All,All,All,2]]];
mean = Accumulate[second]/Range[Length[second]];

Now, I wish to replace the old values in data with this calculated average :

result = {{{{-1,0.2,0}},{{-1,0.35,0}}},
{{{1,0.39,0},{-1,0.2975,0},{1,0.438,0}},{{-1,0.385,0},{-1,0.345714,0},{1,0.3025,0}}},
{{{1,0.28,0},{1,0.293,0},{-1,0.295455,0}},{{1,0.2725,0},{1,0.276923,0},{1,0.257857,0}}},
{{{-1,0.247333,0},{-1,0.238125,0}},{{1,0.248235,0},{-1,0.267778,0}}}};

If the dimensions were known, it wouldn't be a problem. But they aren't. Also, my real dimensions are along the lines of {40, 10000, ?, 3}. I will be running this transformation thousands of time, so speed is key.

Any help is appreciated =)

I have a list dimensions {4, 2, ?, 3}. The ? can be any value from 1 to 5. Here's a simplified example:

data = {{{{-1,0.2,0}},{{-1,0.5,0}}},
{{{1,0.47,0},{-1,0.02,0},{1,1.0,0}},{{-1,0.12,0},{-1,0.11,0},{1,0.,0}}},
{{{1,0.1,0},{1,0.41,0},{-1,0.32,0}},{{1,0.02,0},{1,0.33,0},{1,0.01,0}}},
{{{-1,0.1,0},{-1,0.1,0}},{{1,0.41,0},{-1,0.6,0}}}};

I need to calculate an average progressively on the second value of the last dimension, which I've calculated this way so far:

second = Flatten[data[[All,All,All,2]]];
mean = Accumulate[second]/Range[Length[second]];

Now, I wish to replace the old values in data with this calculated average :

result = {{{{-1,0.2,0}},{{-1,0.35,0}}},
{{{1,0.39,0},{-1,0.2975,0},{1,0.438,0}},{{-1,0.385,0},{-1,0.345714,0},{1,0.3025,0}}},
{{{1,0.28,0},{1,0.293,0},{-1,0.295455,0}},{{1,0.2725,0},{1,0.276923,0},{1,0.257857,0}}},
{{{-1,0.247333,0},{-1,0.238125,0}},{{1,0.248235,0},{-1,0.267778,0}}}};

If the dimensions were known, it wouldn't be a problem. But they aren't. Also, my real dimensions are along the lines of {40, 10000, ?, 3}. I will be running this transformation thousands of time, so speed is key.

EDIT : This is what I've tried and it works, but I'm certain there is a better way

count = 0;
data[[All, All, All, 2]] = Table[++count;
mean[[count]], {n, 4}, {t, 2}, {l, Length[data[[n, t]]]}]
edited tags
Link
Source Link
Lokdal
  • 405
  • 2
  • 8

Replacing elements of irregular lists

I have a list dimensions {4, 2, ?, 3}. The ? can be any value from 1 to 5. Here's a simplified example:

data = {{{{-1,0.2,0}},{{-1,0.5,0}}},
{{{1,0.47,0},{-1,0.02,0},{1,1.0,0}},{{-1,0.12,0},{-1,0.11,0},{1,0.,0}}},
{{{1,0.1,0},{1,0.41,0},{-1,0.32,0}},{{1,0.02,0},{1,0.33,0},{1,0.01,0}}},
{{{-1,0.1,0},{-1,0.1,0}},{{1,0.41,0},{-1,0.6,0}}}};

I need to calculate an average progressively on the second value of the last dimension, which I've calculated this way so far:

second = Flatten[data[[All,All,All,2]]];
mean = Accumulate[second]/Range[Length[second]];

Now, I wish to replace the old values in data with this calculated average :

result = {{{{-1,0.2,0}},{{-1,0.35,0}}},
{{{1,0.39,0},{-1,0.2975,0},{1,0.438,0}},{{-1,0.385,0},{-1,0.345714,0},{1,0.3025,0}}},
{{{1,0.28,0},{1,0.293,0},{-1,0.295455,0}},{{1,0.2725,0},{1,0.276923,0},{1,0.257857,0}}},
{{{-1,0.247333,0},{-1,0.238125,0}},{{1,0.248235,0},{-1,0.267778,0}}}};

If the dimensions were known, it wouldn't be a problem. But they aren't. Also, my real dimensions are along the lines of {40, 10000, ?, 3}. I will be running this transformation thousands of time, so speed is key.

Any help is appreciated =)