IBM Support

Read a 3-dimensional array from an Excel spreadsheet

Question & Answer


Question

How to read a 3-dimensional array from an Excel spreadsheet?

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:

OPL model: 3DimArray.mod



OPL data: 3DimArray.dat



MS Excel file: 3DimArray.xls


 

[{"Type":"MASTER","Line of Business":{"code":"LOB10","label":"Data and AI"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSSA5P","label":"IBM ILOG CPLEX Optimization Studio"},"ARM Category":[{"code":"a8m3p0000006wlmAAA","label":"CPLEX Optimization Studio-\u003EHow to"}],"ARM Case Number":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions"}]

Historical Number

oplstudio/FAQ/1

Document Information

Modified date:
11 April 2022

UID

swg21401340