Array Manipulation Examples and Challenge Problems
This page is a place to collect array-manipulation examples and challenge problems.
If you are learning to use Analytica, try these challenge problems before looking at the solutions.
If you have an example that might be helpful to others, post it!
These should be problems that can be described concisely, and are focused on manipulations of arrays. Beyond the scope would be questions like how to model some particular problem, or how to structure a problem.
List and 1-D manipulations
Reversing a list
Given:Index I, as a list of labels.
Define:Index I_rev containing the same labels as I, but in reverse order.
Solution 1
Define I_rev as
Slice( I, size(I)..1 )
Reversing an array
Given: Array A, indexed by I.
Compute: A_rev, also indexed by I, but with the order of elements along I reversed.
Solution 1
Define A_rev as
A[@I=size(I)-@I+1]
Re-indexing
Table Lookups
Includes 2-step lookup, aka outer-product.
Flattening and Un-flattening
Flattening a 2-D matrix to a 1-D vector
Given: Array A[I,J], indexed by I and J.
Compute: 1-D Array B[K], where K has length size(I)*size(J), containing the elements of A.
Model file: Flattening 2-D to 1-D.ana (contains setup and solutions)
The need to flatten a matrix into 1-D occurs in several contexts. When setting up an optimization, for example, the decision variables must be collected into a single 1-D vector. If you are finding the optimal weight-matrix, for example, you may need to "flatten" your initial guess, in order to pass it to NlpDefine. Similarly, you may have a 2-D array of coefficients identified in a Regression problem, that must be flattened into a 1-D basis index.
Solution 1
Demonstrated in the Solution_1 module of Flattening 2-D to 1-D.ana.
- Add Library..., Concatentation.ana
- Define K as: K := 1..size(I)*size(J)
- Define B as ConcatRows(A,I,J,K)
Solution 2
This is the more general solution, in that it generalized to flattening N-D arrays.
Demonstrated in the Solution_2 module of Flattening 2-D to 1-D.ana.
- Define K as: K := 1..size(I)*size(J)
- Define L := ['I','J','A']
- Define B := MdArrayToTable(A,K,L)[L='A']
Solution 3
Demonstrated in the Solution_3 module of Flattening 2-D to 1-D.ana.
- Define K as: K := 1..size(I)*size(J)
- Define B as: A[ @I = floor((@K-1)/size(J)+1), @J=Mod( (@K-1), size(J) )+1 ]
Unflattening a 1-D vector into a 2-D array
Given: Vector B, indexed by K. Indexes I,J. K has [[size](I)*size(J) elements.
Compute:Array A, indexed by I and J, containing all the elements of B.
Model file: Unflattening 2-D to 1-D.ana (contains setup and one possible solution)
Flattening a symmetrical 2-D matrix into a 1-D vector
Given: Array A[I,J], indexed by I and J.
Compute:1-D Array B[K], containing the elements of the upper-half triangle of the matrix, but without their duplicate lower-half elements.
Model file: Flattening triangular matrix.ana
The model file contains two possible solutions.
Multi-Step Hybrid Challenges
Counting Pairs
Contruct a table of all pairs of contributing factors that occur together, showing how many times the pair occurs and sorted by decreasing frequency.
Given: An array of booleans, B indexed by Failure and Factor, with a 1 indicating that the factor was a contributor to the given failure.
Compute: A table showing each pair of factors that occur together, with how many times that pair was observed to co-occur, and sorted by decreasing count.
Model file: Pair Counting.ana
The model contains the starting data, the desired result, and one possible solution broken down step-by-step. For the full challenge, find a way to compute the solution yourself without looking. If you aren't up for that, then take on the four individual steps in the solution as four smaller challenge exercises.
Enable comment auto-refresher