Question & Answer
Question
Answer
Data in spreadsheets and relational databases is usually 2 dimensional. So data with 3 or more dimensions cannot be stored in spreadsheets or relational databases natively. One way to store and read such data is to flatten the data from multiple dimensions into two dimensions by grouping the dimensions into two groups: one group for one dimension of the flattened data.
The sample shows how 3-dimensional data is flattened into 2-dimensional data. The original data has three dimensions: nbMonths, nbProducts, and nbLevels. The flattened data has two dimensions: one corresponds to nbMonths, and the other dimension corresponds to the group of nbProducts and nbLevels. The sample uses an array to read in the flattened data first, by calling function SheetRead(), then initializes multi-dimensional data from the flattened data.
The model file (.mod)
int nbMonths = ...;
int nbProducts = ...;
int nbLevels = ...;
// If the data elements are organized in columns
int nb1[1..nbMonths*nbProducts*nbLevels] = ...;
int nbArray[m in 1..nbMonths, p in 1..nbProducts,s in1..nbLevels]=
nb1[s+nbLevels*(p-1)+nbProducts*nbLevels*(m-1)];
// If the data elements are in a 2-dimensional array
int nb2[1..nbMonths, 1..nbProducts*nbLevels] = ...;
int nbArray2[m in 1..nbMonths, p in 1..nbProducts,s in 1..nbLevels] =
nb2[m,s+nbLevels*(p-1)];
The data file (.dat)
SheetConnection sheetInput("3DimArray.xls", 0);
nb1 from SheetRead(sheetInput,"data!D2:D13");
nb2 from SheetRead(sheetInput,"data!B17:G18");
Download the model and data files:
Historical Number
oplstudio/FAQ/1
Was this topic helpful?
Document Information
Modified date:
11 April 2022
UID
swg21401340