首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用布尔数组进行数组索引

用布尔数组进行数组索引
EN

Stack Overflow用户
提问于 2019-05-15 13:59:55
回答 1查看 86关注 0票数 1

给定一个实数组(例如myArray)和一个布尔数组(例如myMask),我希望有:

  • 如果myMask[i] == true那么myArray[i] = myValueTrue
  • 如果myMask[i] == false那么myArray[i] = myValueFalse

这个的工作

代码语言:javascript
复制
model BooleanIndexing
  parameter Boolean myMask[3] = {false, true, true};
  parameter Boolean myMask_negated[3] = {true, false, false};
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation
  myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
  myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] = fill(myValueFalse, Modelica.Math.BooleanVectors.countTrue(myMask_negated));
end BooleanIndexing;

但是这个

代码语言:javascript
复制
model BooleanIndexing
  parameter Boolean myMask[3] = {false, true, true};
  parameter Boolean myMask_negated[3] = not myMask;
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation
  myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
  myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] = fill(myValueFalse, Modelica.Math.BooleanVectors.countTrue(myMask_negated));
end BooleanIndexing;

唯一的区别是我如何初始化myMask_negated

在OpenModelica中,这些错误是:

代码语言:javascript
复制
[BooleanIndexing: 9:3-9:139]: Illegal subscript Modelica.Math.BooleanVectors.index({myMask_negated[1], myMask_negated[2], myMask_negated[3]}) for dimensions 3 in component myArray[Modelica.Math.BooleanVectors.index(myMask_negated)].

[BooleanIndexing: 9:3-9:139]: Variable myArray[Modelica.Math.BooleanVectors.index(myMask_negated)] not found in scope BooleanIndexing.

Error occurred while flattening model BooleanIndexing

在Dymola2018中

代码语言:javascript
复制
Translation of BooleanIndexing:

Failed to expand myArray[Modelica.Math.BooleanVectors.index(myMask)].

Errors or failure to expand the equation:
myArray[Modelica.Math.BooleanVectors.index(myMask)] = fill(myValueTrue, Modelica.Math.BooleanVectors.countTrue(myMask));
Found in class BooleanIndexing, C:/workspace/modelica_vehicle/modelica_test/BooleanIndexing.mo at line 8.

Errors or failure to expand vector or matrix expressions.

Translation aborted.

直接布尔索引myArray[myMask]似乎不是这里的解决方案。我看不出他们为什么会失败,是否还有更优雅的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-16 07:41:08

这两个版本都不能保证生成正确数量的方程式。myValueTruemyValueTrue_negated都是参数,因此用户可以将向量的值更改为不互补的值。

因此,我建议

代码语言:javascript
复制
final parameter Boolean myMask_negated[3] = not myMask;

但这也不起作用,无论是在还是Dymola。

因此,我建议删除myMask_negated并使用for循环。或者有两个单独的:

代码语言:javascript
复制
model BooleanIndexing
  parameter Boolean myMask[3] = {false, true, true};
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation 
  for i in Modelica.Math.BooleanVectors.index(myMask) loop
    myArray[i] = myValueTrue;
  end for;

  for i in Modelica.Math.BooleanVectors.index(not myMask) loop
    myArray[i] = myValueFalse;
  end for;

end BooleanIndexing;

或者使用数组构造函数使用单个for循环,如下所示:

代码语言:javascript
复制
model BooleanIndexing2
  parameter Boolean myMask[3] = {false, true, true};
  Real myArray[3];
  parameter Real myValueTrue = 3.0;
  parameter Real myValueFalse = 5.0;
equation 

  myArray = {if value then myValueTrue else myValueFalse for value in myMask};

end BooleanIndexing2;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56151194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档