如何在另一个向量中插入向量的参数? [英] How to insert arguments of a vector inside another vector?

查看:74
本文介绍了如何在另一个向量中插入向量的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量"barycenter".尺寸为"5":

 参数长度重心[5] = {1,2,3,4,5}; 

,我想将此向量的参数添加到大小为"7"的另一个向量(b_prime)内,这意味着打算使b_prime = {0,1,2,3,4,5,10}.如何在Modelica中编写代码行来做到这一点?我尝试了以下操作,但收到错误:

 参数长度b_prime [7] = {0,重心[1:5],10}; 

 参数长度b_prime [7] = {0,重心,10}; 

感谢您的帮助.

解决方案

下面的代码应执行您想要的操作:

 模型CombineVectors导入Modelica.SIunits.Length;参数长度重心[5] = {1,2,3,4,5};参数Length b_prime [7] = cat(1,{0},重心,{10});end CombineVectors; 

需要注意的两个重要事项:

  1. cat()的第一个参数是应合并数组的维数
  2. 标量放在大括号中以使其成为矢量.这样做是为了满足第一个规则".在下面找到.因此,除第一个参数外, cat()的每个参数都具有一个维度.

来自 Modelica语言规范,第10.4.2节数组串联:

函数 cat(k,A,B,C,...)将维度 k的数组 A,B,C,... 串联在一起根据以下规则:

  • 数组 A,B,C,... 必须具有相同的维数,即 ndims(A)= ndims(B)= ...
  • 数组 A,B,C,... 必须是类型兼容的表达式(第6.6节),给出结果元素的类型.最大扩展类型应等效.可以将Real和Integer子类型混合使用,以生成一个Real结果数组,其中将Integer数转换为Real数.
  • k必须表征一个现有尺寸,即 1< = k< = ndims(A)= ndims(B)= ndims(C); k 应为整数.
  • 大小匹配:数组 A,B,C,... 必须具有相同的数组大小,但维度 k 的大小除外,即size(A,j)= size(B,j),对于 1< = j< = ndims(A)和j<>k .

I have a vector of "barycenter" with size "5":

parameter Length barycenters[5] = {1, 2, 3, 4, 5};

and I would like to add the arguments of this vector inside another vector (b_prime) with size "7", meaning that intend to have b_prime = {0, 1, 2, 3, 4, 5, 10}. How is it possible to write a code line in Modelica to do so? I have tried the followings but I received errors:

parameter Length b_prime[7] = {0, barycenters[1:5], 10}; 

or

parameter Length b_prime[7] = {0, barycenters, 10}; 

I would appreciate your help.

解决方案

The following code should do what you want:

model CombineVectors
  import Modelica.SIunits.Length;

  parameter Length barycenters[5] = {1, 2, 3, 4, 5};
  parameter Length b_prime[7] = cat(1, {0}, barycenters, {10});

end CombineVectors;

Two important things to notice:

  1. The first argument of cat() is the dimension in which the arrays shall be concatinated
  2. The scalars are put in curly brackets to make them a vector. This is done to fulfill the first of the "rules" found below. Therefore every argument of cat() except the first has the one dimension.

From the Modelica Language Specification, Section 10.4.2 Array Concatenation:

The function cat(k,A,B,C,...) concatenates arrays A,B,C,... along dimension k according to the following rules:

  • Arrays A, B, C, ... must have the same number of dimensions, i.e., ndims(A) = ndims(B) = ...
  • Arrays A, B, C, ... must be type compatible expressions (Section 6.6) giving the type of the elements of the result. The maximally expanded types should be equivalent. Real and Integer subtypes can be mixed resulting in a Real result array where the Integer numbers have been transformed to Real numbers.
  • k has to characterize an existing dimension, i.e., 1 <= k <= ndims(A) = ndims(B) = ndims(C); k shall be an integer number.
  • Size matching: Arrays A, B, C, ... must have identical array sizes with the exception of the size of dimension k, i.e., size(A,j) = size(B,j), for 1 <= j <= ndims(A) and j <> k.

这篇关于如何在另一个向量中插入向量的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆