当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Matlab AMPL DataFrame.setMatrix用法及代码示例


classmethod DataFrame.setMatrix()

用法

setMatrix(value, {indices1, ..., indicesn})

说明

setMatrix(matrix, {indices1, ..., indicesn}) 将 MATLAB n 维矩阵的所有值与索引值一起分配给此 DataFrame。

输入参数

matrix 包含值的矩阵。它也可以是包含字符串的 n 维元胞数组。请注意,在当前版本中,最多支持六个维度的矩阵。

{indices1, ... indicesn}一个单元格数组,包含n向量在哪里n是矩阵的维数。每个向量必须具有相同的大小matrix在那个维度。

示例1:一维

创建一个一维矩阵(一个数组)并分配它

% Create a vector of four random numbers
A = randn(4,1);
% Create a vector of indices
A1 = {'a1' , 'a2', 'a3' , 'a4'};
% Create a dataframe with 1 index
df = DataFrame(1, 'A1', 'Values');
% Assign the vector
df.setMatrix(A, A1)
% Print the value
df

给出:

df =
A1  |  Values
a1  |  0.3187652398589808
a2  |  -1.3076882963052734
a3  |  -0.43359202230568356
a4  |  0.3426244665386499

示例2:二维和分配给 AMPL 参数

创建一个二维矩阵并使用 AMPL.setData 将其分配给 AMPL 参数。 DataFrame 和分配值的参数之间的绑定是通过 DataFrame 中唯一数据列的名称实现的

% Create a 4x3 matrix
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
% First index (cardinality = number of rows
A1 = {'a1' ; 'a2'; 'a3' ; 'a4'};
% Second index (cardinality = number of columns)
A2 = {1; 2; 3};
% Create a dataframe with two idexing columns and
% a data column called TheParameter
df = DataFrame(2, 'A1', 'A2', 'TheParameter');
% Assign the matrix to the dataframe
df.setMatrix(A, A1, A2)

% Create an AMPL instance
ampl = AMPL;
% Create two sets and a parameter. The name of the parameter
% has to be the same as the column in our dataframe.
% Sets name can differ, since we are not assigning them using
% AMPL.setData
ampl.eval('set S1; set S2; param TheParameter{S1, S2};');
% Set the data for sets S1 and S2
ampl.getSet('S1').setValues(A1);
ampl.getSet('S2').setValues(A2);
ampl.setData(df);
ampl.display('TheParameter')

给出:

TheParameter :=
   a1 1    1
   a1 2    2
   a1 3    3
   a2 1    4
   a2 2    5
   a2 3    6
   a3 1    7
   a3 2    8
   a3 3    9
   a4 1   10
   a4 2   11
   a4 3   12
   ;

示例3:符号参数赋值

此示例说明如何将包含字符串的二维元胞数组分配给参数。

% Create a 2-d cell array
cellArray = {'r1c1', 'r1c2', 'r1c3'; 'r2c1', 'r2c2', 'r2c3'; 'r3c1', 'r3c2','r3c3'; 'r4c1', 'r4c2', 'r4c3'}
% assign it to the previous dataframe
df.setMatrix(cellArray, A1, A2)
% display the dataframe
df

给出:

df =
A1  A2   |  TheParameter
a1  1.0  |  r1c1
a1  2.0  |  r1c2
a1  3.0  |  r1c3
a2  1.0  |  r2c1
a2  2.0  |  r2c2
a2  3.0  |  r2c3
a3  1.0  |  r3c1
a3  2.0  |  r3c2
a3  3.0  |  r3c3
a4  1.0  |  r4c1
a4  2.0  |  r4c2
a4  3.0  |  r4c3

示例4:高维矩阵

此示例显示如何将 4 维矩阵分配给 DataFrame 相同的逻辑适用于高维元胞数组。

% Create a 4-d matrix
B = randn(2,2,2,2);
% Create 4 sets of indices of cardinality 2 and of various types
% (both strings and numbers)
B1 = {'a1', 4};
B2 = {'1', '4'};
B3 = {'c1' ,'2'};
B4 = {4, '4'};
% Create a dataframe with 4 index columns
df = DataFrame(4, 'B1', 'B2', 'B3', 'B4', 'Value')
% Assign the valuees to the dataframe
df.setMatrix(B, B1, B2, B3, B4)
% Display the contents
df

给出:

df =
B1   B2  B3  B4   |  Value
a1   1   c1  4.0  |  -1.0688704581680317
a1   1   c1  4    |  -0.10224244608549089
a1   1   2   4.0  |  0.32519053945619786
a1   1   2   4    |  -0.8648799173244565
a1   4   c1  4.0  |  -2.9442841619948963
a1   4   c1  4    |  0.3192067391655018
a1   4   2   4.0  |  1.370298540095228
a1   4   2   4    |  -0.1648790192090383
4.0  1   c1  4.0  |  -0.8094986944248755
4.0  1   c1  4    |  -0.24144704160735794
4.0  1   2   4.0  |  -0.7549283191697034
4.0  1   2   4    |  -0.03005129619626856
4.0  4   c1  4.0  |  1.4383802928150984
4.0  4   c1  4    |  0.31285859663742843
4.0  4   2   4.0  |  -1.711516418853698
4.0  4   2   4    |  0.6277072875287265

相关用法


注:本文由纯净天空筛选整理自ampl.com大神的英文原创作品 setMatrix。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。