add and derive average using javascript and structures

Swiftlikeninja's picture

Let me start off by saying that I am a complete noob when it comes to javascript. Id imagine that this should be easy to accomplish.

I would like to know how to take a structure and add up all the values then divide the total by the structure count providing me with an average. Currently I am accomplishing this with a rather ugly and lengthy set of structure makers to a math patch but its not dynamic, and is pretty sloppy. To go one step further I would like to be able to add up all of a certain sub index within a complete structure through javascript. Im not concerned with maintaining the original structure, the only output needed is the average value.

Im not asking anyone to make this for me but again im lost when it comes to javascript and would appreciate any assistance. Ive dugged through the post to attempt to find an answer got nowhere. Ive checked cybero's reference site but cant figure out what to combine to achieve what I need.

toneburst's picture
Re: add and derive average using javascript and structures

Hiya,

See attached.

a|x

PreviewAttachmentSize
average struct.qtz8.36 KB

cybero's picture
Re: add and derive average using javascript and structures

Sorry missed out on this as I had a late Wednesday, crashed early and didn't even get to to catch the England v Egypt friendly yesterday.

That's an interesting and elegantly simple approach, toneburst.

How useful is it to actually average a dynamic [changeable] structure though ?

Not saying that to be a downer BTW, just occurred to me as I took my late breakfast - :-)

If I averaged the x,y &z positions for each member of a given structure over n seconds of production surely I'll end up getting, perhaps, a rather odd looking 'sphere' or whatever I was supposed to be producing in the first instance when each of those structure members x, y & z are averaged..

Plus it would be static.

So why not then simply produce a static array in the first instance?

Still - the fact that I don't see the point [ no pun intended] on this doesn't mean to say that there isn't actually one.

Post Script

Upon reflection I can see how one could average , as in smooth structures, just look at the Smooth Structure script in the music visualizer template.

Definitely comes a little into it's own for visualizing dynamic structures a little more easily, thanks to the smoothing [& sadly a little dumbed down too].

Also good for a dumbed down of trajectories, I guess.

New [From Template] / Music Visualizer.

PreviewAttachmentSize
sortsmooth.qtz11.63 KB

cybero's picture
Re: add and derive average using javascript and structures

Swiftlikeninja, would like to know, what sort of structure - to begin with?

Is it dynamic [fixed or changing number of members] or simply multi-faceted , albeit static?

Swiftlikeninja's picture
Re: add and derive average using javascript and structures

Cybero, It is a multifaceted dynamic structure. In example; the structure will always have substructures 0-4 and im only focusing on index 1 subindex 2 and index 2 subindex 2 etc all the way until the end of the structure. The structure layout will always be the same but the values within will be dynamic.

Im using this javascript for data collection and returns, kinda like a dynamic scorecard

Swiftlikeninja's picture
Re: add and derive average using javascript and structures

Thanks Toneburst, this is exactly what i needed albeit without the substructure ability but either ill figure out that part on my own or find a work around.

Swiftlikeninja's picture
Re: add and derive average using javascript and structures

After a little (ALOT) of trail and error i managed to do something in javascript. I just slightly modified your code toneburst to include a sub index indicator and it digs through the structure and averages the give substructure index. Thanks alot, could not have done it without you. I've attached the updated code.

PreviewAttachmentSize
average substructure.qtz10.52 KB

cybero's picture
Re: add and derive average using javascript and structures

Been busy elsewhere so just picked up on this to find that you've squared the circle on this one already.

I'd have thought you'd be looking for the average of each 0,1,2 & 3, rather than a total average as the posted draft shows, though.

toneburst's picture
Re: add and derive average using javascript and structures

Swiftlikeninja didn't say whether they were talking about 2-Dimensional structures or not. I assumed not. The same code would equally work with 2D structures- you'd just need to nest another loop to go through each sub-array.

a|x