VECTOR SELECT(selection_array, expression_array, missing_vals, numerical_action, error_action) SELECT some elements of a VECTOR |
Scroll Prev Top Next More |
The VECTOR SELECT function provides a convenient mechanism for selecting values from elements in an array.
Function arguments
selection_array |
This specifies a selection array with a mixture of zeroes and non-zero values. |
expression_array |
This is the expression that elements are being selected from based on the selection array. |
missing_vals |
The value to use in the case where there are only zeroes in the selection array. |
numerical_action |
The action to take. See below for a more detailed explanation. |
error_action |
Indicates how to treat too many or too few entries in the selection. See below for a more detailed explanation. |
numerical_action can take the following values
0
|
A value of zero will calculate the weighted sum. |
1
|
When values in the selection array are non-zero, this will calculate the product of the selection_array * expression_array. |
2
|
The weighted minimum, for non zero values of the selection array, this is minimum of selection_array * expression_array. |
3
|
The weighted maximum, for non zero values of the selection array, this is maximum of selection_array * expression_array. |
4
|
For non zero values of the selection array, this is the average of selection_array * expression_array. |
5
|
When values in the selection array are non-zero, this will calculate the product of the selection_array ^ expression_array. |
6
|
When values in the selection array are non-zero, this will calculate the sum of the expression_array. The same as the SUM function for non-zero values in the selection array. |
7 |
When values in the selection array are non-zero, this will calculate the product of the expression_array. The same as the PROD function for non-zero values in the selection array.
|
8 |
The unweighted minimum, for non zero values of the selection array, this is minimum of the expression_array. The same as the VMIN function for non-zero values in the selection array.
|
9 |
The unweighted maximum, for non zero values of the selection array, this is maximum of expression_array. The same as the VMAX function for non-zero values in the selection array.
|
10 |
For non zero values of the selection array, this is the average of expression_array.
|
error_action tells Vensim how to treat too many or too few entries in the selection array. It can take the following values.
0 |
No error is raised. |
1 |
Raise a floating point error is selection array only contains zeros. |
2 |
Raise an error if the selection array contains more than one non-zero value. |
3 |
Raise an error if all elements in selection array are zero, or more than one element is non-zero (this is a combination of error_action = 1 and error_action = 2).
|
The VECTOR SELECT function is very similar to formulations such as:
result[row] = SUM(sarray[row,col!]*expr[col!])
but has two important differences. The first is that causal tracing, instead of indicating that every element of expr is a cause of every element of result will only display those elements where there is a nonzero element in sarray. The second is that the expression that appears as the second argument is only evaluated when it will contribute to the computation and not for every possible row and column combination.
Related to the second of these, since some elements of vexp might not be used, Vensim does not verify that all are actually computed. Thus "Used not computed" error messages will not be generated for variables involved in the vexp argument of the VECTOR SELECT function (all other arguments, including sarray are tested). If an array element is not computed it has the value :NA: which will normally lead to a floating point error or visibly anomalous results during simulation should it be used. However, extra caution is called for when making use of the VECTOR SELECT function.
Note that causal tracing is only changed for the Strip Graph and Table tools. The structural tools do not reference subscripts. When tracing causes using simulation results if there are no runs loaded, or if the loaded runs do not contain values for sarray, then no causes will be listed. If sarray is different in different runs then the union of all possible causes will be displayed. In this case some of the causes will not be applicable to some of the runs, you will need to check the values of sarray to see which apply.
VECTOR SELECT is very useful for prerequisite mapping as well as compression and expansion of array sizes as shown in the examples. For the first two of these err would normally be set to 1 while for vector expansion err would normally be set to 3. If err is 0 or 2 and there are no non-zero elements then the function will return mval. If err is 1 or 3 then mval will never be used and a value of :NA: is recommended.
If sarray is an Unchangeable Constant (defined using ==) then the causality of VECTOR SELECT used in determining equation order is based on only the nonzero elements of sarray. This provides a convenient mechanism for propagating dependencies between subscript elements. An example of this for a project is included below.
Example
In order to determine whether all prerequisite tasks are completed:
task : t1,t2,t3,t4
prereq <-> task
prereq map[task,prereq] ==
0,0,0,0;
1,0,0,0;
1,1,0,0;
0,0,1,0;
prereqs are complete[task] = VECTOR SELECT( |
prereq map[task,prereq!], |
task is complete[prereq!],1,2,0) |
Note that the first row of prereq mat is all 0s – so for t1 prereq is complete will use the 3rd argument to the function or 1 which is what we want since t1 does not have any prerequisites.
Example
Quality depends on both work quality and the quality of prerequisite work products. Using the above task and prerequisite definitions:
task quality[task] = work quality[task] * prereq quality[task]
work quality[task] = .8,.9,.7,.9
prereq quality[task] = VECTOR SELECT( |
prereq map[task,prereq!], |
task quality[prereq!],1,1,0) |
For this to work we depend on prereq map being defined as an Unchangeable Constant. If it weren’t, then there would be a simultaneous equation involving work quality and task quality.
Example
To move between cohort aggregations and disaggregations:
Age : a1,a2,a3,a4,a5,a6
AgeGroup : ag1,ag2,ag3
age grouping map[AgeGroup,Age] =
1,1,0,0,0,0;
0,0,1,1,0,0;
0,0,0,0,1,1;
population grouped[AgeGroup] = VECTOR SELECT( |
age grouping map[AgeGroup,Age!], |
population[Age!], |
:NA:,0,1) |
effect pollution deaths by age[Age] = VECTOR SELECT( |
age grouping map[AgeGroup!,Age], |
effect pollution deaths by age group[AgeGroup!], |
:NA:,0,3) |
Example
Simplified heat transfer on a two dimensional surface. This uses two subscripts to select from and also treats the edge determination using VECTOR SELECT. The
x:(x1-x3)
y:(y1-y4)
edge : left,right,top,bottom
xp <-> x
yp <-> y
average adjacent temp diff[x,y] = (
VECTOR SELECT(adjacency matrix[x,y,xp!,yp!],
temp[xp!,yp!]-temp[x,y],:NA:,0,1) +
VECTOR SELECT(edge matrix[x,y,edge!],
edge temp[edge!]-temp[x,y],0,0,0)
) / 4
adjacency matrix[x,y,xp,yp] = INITIAL(
IF THEN ELSE((yp = y+1 :AND: xp = x) :OR:
(yp = y-1 :AND: xp = x) :OR:
(xp = x+1 :AND: yp = y) :OR:
(xp = x-1 :AND: yp = y),
1,0))
edge matrix[x,y,edge] = INITIAL(
IF THEN ELSE((y = 1 :AND: edge = top) :OR:
(y = elmcount(y) :AND: edge = bottom) :OR:
(x = 1 :AND: edge = left) :OR:
(x = elmcount(x) :AND: edge = right),
1,0))
Note that the VECTOR SELECT function does not use average but sums for this computation. Since some of the 4 adjacent positions might come from edges and some from other cells the averaging is done outside of the function (with simply /4).
Units: VECTOR SELECT(unit1,unit2,units,dmnl,dmnl) --> units=units1*units2
Restrictions: Both sarr and vexpr must have at least one subscript with an !. Normally there will be one subscript acted on with the ! and that subscript will be that is the same in both sarr and vexpr but this is not a requirement.
Availability: Professional and DSS only.
See also: PROD, SUM, VMIN and VMAX.
Sample model: VECTOR SELECT.mdl in FunctionExamples