當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。