在MATLAB中计算总和的快速方法是什么?

恩辛

所以我有以下限制: 在此处输入图片说明

如何在MATLAB中高效地编写代码?的输入是x_mnM,和N集合B={1,...,N}和集合U={1,...,M}

我是这样做的(因为我写x为follwoing向量)

x=[x_11, x_12, ..., x_1N, X_21, x_22, ..., x_M1, X_M2, ..., x_MN]

 %# first constraint
 function R1 = constraint_1(M, N)

 ee  = eye(N);
 R1  = zeros(N, N*M);
 for m = 1:M  
     R1(:, (m-1)*N+1:m*N) = ee;
 end

 end

 %# second constraint
 function R2 = constraint_2(M, N)

 ee = ones(1, N);
 R2 = zeros(M, N*M);
 for m = 1:M
     R2(m, (m-1)*N+1:m*N) = ee;
 end

 end

通过上面的代码,我会得到一个矩阵A=[R1; R2]0-1我都会有A*x<=1

例如,,M=N=2我将得到以下内容:在此处输入图片说明

并且,我将根据创建一个test(x)返回true或false的函数x

我想从您那里得到一些帮助并优化我的代码。

雷瑞恩

You should place your x_mn values in a matrix. After that, you can sum in each dimension to get what you want. Looking at your constraints, you will place these values in an M x N matrix, where M is the amount of rows and N is the amount of columns.

You can certainly place your values in a vector and construct your summations in the way you intended earlier, but you would have to write for loops to properly subset the proper elements in each iteration, which is very inefficient. Instead, use a matrix, and use sum to sum over the dimensions you want.

For example, let's say your values of x_mn ranged from 1 to 20. B is in the set from 1 to 5 and U is in the set from 1 to 4. As such:

X = vec2mat(1:20, 5)

X =

 1     2     3     4     5
 6     7     8     9    10
11    12    13    14    15
16    17    18    19    20

vec2mat takes a vector and reshapes it into a matrix. You specify the number of columns you want as the second element, and it will create the right amount of rows to ensure that a proper matrix is built. In this case, I want 5 columns, so this should create a 4 x 5 matrix.

The first constraint can be achieved by doing:

first = sum(X,1)

first =

 34    38    42    46    50

sum works for vectors as well as matrices. If you have a matrix supplied to sum, you can specify a second parameter that tells you in what direction you wish to sum. In this case, specifying 1 will sum over all of the rows for each column. It works in the first dimension, which is the rows.

What this is doing is it is summing over all possible values in the set B over all values of U, which is what we are exactly doing here. You are simply summing every single column individually.

The second constraint can be achieved by doing:

second = sum(X,2)

second =

 15
 40
 65
 90

Here we specify 2 as the second parameter so that we can sum over all of the columns for each row. The second dimension goes over the columns. What this is doing is it is summing over all possible values in the set U over all values of B. Basically, you are simply summing every single row individually.

BTW, your code is not achieving what you think it's achieving. All you're doing is simply replicating the identity matrix a set number of times over groups of columns in your matrix. You are actually not performing any summations as per the constraint. What you are doing is you are simply ensuring that this matrix will have the conditions you specified at the beginning of your post to be enforced. These are the ideal matrices that are required to satisfy the constraints.

Now, if you want to check to see if the first condition or second condition is satisfied, you can do:

%// First condition satisfied?
firstSatisfied = all(first <= 1);

%// Second condition satisfied
secondSatisfied = all(second <= 1);

这将检查firstor的每个元素,second并查看您执行上面刚刚显示的代码后得到的总和是否全部<= 1如果他们都满足此约束,我们将拥有true否则,我们有false

如果您还需要其他任何信息,请告诉我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在MATLAB中计算此总和的快速方法是什么?

来自分类Dev

在omnet ++中计算延迟的正确方法是什么?

来自分类Dev

在C ++中计算矩阵秩的最快方法/库是什么

来自分类Dev

从php提交数组中计算$ _POST的方法是什么

来自分类Dev

在GraphX中计算图直径的正确方法是什么

来自分类Dev

是什么会导致Excel状态栏快速总和计算错误?

来自分类Dev

从大型点数据集中计算附近点的最快方法是什么

来自分类Dev

在node.js中计算Content-length标头的正确方法是什么

来自分类Dev

在python中使用networkx在无向图中计算大小k的集团的最佳方法是什么?

来自分类Dev

在数组中计算均值的更有效方法是什么?

来自分类Dev

在SQL中计算滚动聚合的最有效方法是什么?

来自分类Dev

在特定日期(mysql-php)中计算特定值的最简单方法是什么?

来自分类Dev

在Ruby on Rails中计算TIMESTAMP值之间的差异的最佳方法是什么

来自分类Dev

从大型点数据集中计算附近点的最快方法是什么

来自分类Dev

在 DataTable 列中计算总数的最有效方法是什么

来自分类Dev

在 Perl 中计算 32mb 文件的熵 - 最快的方法是什么?

来自分类Dev

重新组合结果并从查询集中计算它们的最佳方法是什么?

来自分类Dev

使用pandas在python中计算列总和的方法

来自分类Dev

在 R 中计算这个三重总和的最快方法

来自分类Dev

foreach中计算列的总和

来自分类Dev

在PostgreSQL中计算每日总和

来自分类Dev

在Matlab中计算函数

来自分类Dev

在Cassandra中计算行中的列数的方式是什么?

来自分类Dev

在Excel中计算反向CAGR的公式是什么?

来自分类Dev

用Python在字符串中计算后搜索数字并将其替换的最佳方法是什么?

来自分类Dev

Excel中计算时间表中特定小时数以上的工作日数最简单的方法是什么?

来自分类Dev

有效地在Matlab中计算两倍总和?

来自分类Dev

在MATLAB中计算海量数据均值的有效方法

来自分类Dev

如何在Matlab中计算高斯siedel方法的迭代

Related 相关文章

  1. 1

    在MATLAB中计算此总和的快速方法是什么?

  2. 2

    在omnet ++中计算延迟的正确方法是什么?

  3. 3

    在C ++中计算矩阵秩的最快方法/库是什么

  4. 4

    从php提交数组中计算$ _POST的方法是什么

  5. 5

    在GraphX中计算图直径的正确方法是什么

  6. 6

    是什么会导致Excel状态栏快速总和计算错误?

  7. 7

    从大型点数据集中计算附近点的最快方法是什么

  8. 8

    在node.js中计算Content-length标头的正确方法是什么

  9. 9

    在python中使用networkx在无向图中计算大小k的集团的最佳方法是什么?

  10. 10

    在数组中计算均值的更有效方法是什么?

  11. 11

    在SQL中计算滚动聚合的最有效方法是什么?

  12. 12

    在特定日期(mysql-php)中计算特定值的最简单方法是什么?

  13. 13

    在Ruby on Rails中计算TIMESTAMP值之间的差异的最佳方法是什么

  14. 14

    从大型点数据集中计算附近点的最快方法是什么

  15. 15

    在 DataTable 列中计算总数的最有效方法是什么

  16. 16

    在 Perl 中计算 32mb 文件的熵 - 最快的方法是什么?

  17. 17

    重新组合结果并从查询集中计算它们的最佳方法是什么?

  18. 18

    使用pandas在python中计算列总和的方法

  19. 19

    在 R 中计算这个三重总和的最快方法

  20. 20

    foreach中计算列的总和

  21. 21

    在PostgreSQL中计算每日总和

  22. 22

    在Matlab中计算函数

  23. 23

    在Cassandra中计算行中的列数的方式是什么?

  24. 24

    在Excel中计算反向CAGR的公式是什么?

  25. 25

    用Python在字符串中计算后搜索数字并将其替换的最佳方法是什么?

  26. 26

    Excel中计算时间表中特定小时数以上的工作日数最简单的方法是什么?

  27. 27

    有效地在Matlab中计算两倍总和?

  28. 28

    在MATLAB中计算海量数据均值的有效方法

  29. 29

    如何在Matlab中计算高斯siedel方法的迭代

热门标签

归档