# {Question} SImple Array Average

## Recommended Posts

So this is where my Beauty School DropOut level math fails me - I need to count and average the Skill of any given group on the map.

##### Share on other sites

These seem to be working, give them a shot:

syntax: x = skill unitName

Spoiler
```
/* ---------------------------------------------------------------
returns average skill for all units in a group.
The 'skill' command syntax used is: _value = skill _unitName;
Example of use, compiled as a function and called:

getSkillAverage = compile preprocessfilelinenumbers "getSkillAverage.sqf";

_skillAverage = _group call getSkillAverage;

returns value averaged for the group, example:	3.563425
------------------------------------------------------------------ */
ScriptName "getSkillAverage.sqf";

Private ["_grp","_units","_skills","_total","_count","_skillAverage"];
_grp = _this;
_units = Units _grp;
_skills = [];

// Get an array of each unit's skill level.
{_skills = _skills + [skill _x];} forEach _units;

// Get the total skill combined.
_total = 0;
_count = Count _skills;
{_total = _total + _x} forEach _skills;

// Divide by the number of each.
_skillAverage = (_total / _count);

// Return the value.
_skillAverage
```

syntax: x = unitName skill skillType

Spoiler
```
/* ---------------------------------------------------------------
returns skillsets array averaged for all units in a group.
The 'skill' command syntax used is: _value = unitName skill skillType;
Example of use, compiled as a function and called:

getEachSkillAverage = compile preprocessfilelinenumbers "getEachSkillAverage.sqf"

_skillAverages = _group call getEachSkillAverage;

returns averages for the group:
[_aimingAccuracy,	select 0;
_aimingShake,		select 1;
_aimingSpeed,		select 2;
_endurance,		select 3;
_spotDistance,		select 4;
_spotTime,		select 5;
_courage,		select 6;
_commanding,		select 8;
_general]		select 9;
------------------------------------------------------------------ */
ScriptName "getEachSkillAverage.sqf";

Private ["_grp","_units","_skills","_skillNames","_eachSkillAverage","_skillSet","_unit","_index","_count","_total","_average"];
_grp = _this;
_units = Units _grp;
_skills = [];
_eachSkillAverage = [0,0,0,0,0,0,0,0,0,0];

// Get an array containing arrays of each unit's individual skills.
{
_skills = _skills + [[]];
_skillSet = _skills select (count _skills - 1);
_unit = _x;
{_skillSet = _skillSet + [_unit Skill _x]} forEach _skillNames;
_skills set [(count _skills - 1),_skillSet];
} forEach _units;

// Combine into an array containing total for all units' individual skills
_index = 0;
{
_skillSet = _skills select _index;
_count = 0;
{
_eachSkillAverage set [_count,((_eachSkillAverage select _count) + (_skillSet select _count))];
_count = _count + 1;
} forEach _skillSet;
_index = _index + 1;
} forEach _skillNames;

// Divide each skill amount by the number of units.
_index = 0;
{
_total = _eachSkillAverage select _index;
_average = (_total / (count _units));
_eachSkillAverage set [_index,_average];
_index = _index + 1;
} forEach _skillNames;

// Return the array.
_eachSkillAverage
```

Edited by opusfmspol
• 1

##### Share on other sites
```{
_x setVariable ["grpMean", (units _x apply {skill _x}) call BIS_fnc_arithmeticMean ]
} forEach allGroups;```

recall the value by

_mean = _yourGroup getVariable "grpMean"

• 1
• 1

##### Share on other sites

Elegant solution pierremgi -many thanks

BIS_fnc_arithmeticMean -where do you even find this stuff?!

##### Share on other sites
```private _result = allGroups apply {private _acc = 0; {_acc = _acc + skill _x} forEach units _x; _acc / count units _x}
// Result: [0.4375,0.4375,1]```
```private _result = allGroups apply {private _acc = 0; {_acc = _acc + skill _x} forEach units _x; [_x, _acc / count units _x]}
// Result: [[B Alpha 1-1,0.4375],[B Alpha 1-2,0.4375],[B Alpha 1-3,1]]```

• 2

##### Share on other sites
9 hours ago, opusfmspol said:

These seem to be working, give them a shot:

These are great man - i actually needed a subSkills breakdown as well -this REALLY helps thanks man!