所以我有以下限制:
如何在MATLAB中高效地编写代码?的输入是x_mn
,M
,和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);
这将检查first
or的每个元素,second
并查看您执行上面刚刚显示的代码后得到的总和是否全部<= 1
。如果他们都满足此约束,我们将拥有true
。否则,我们有false
。
如果您还需要其他任何信息,请告诉我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句