npisinp
npisinp

Reputation: 370

What is the fast way to calculate this summation in MATLAB?

So I have the following constraints: enter image description here

How to write this in MATLAB in an efficient way? The inputs are x_mn, M, and N. The set B={1,...,N} and the set U={1,...,M}

I did it like this (because I write x as the follwoing vector)

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

By the above code I will get a matrix A=[R1; R2] with 0-1 and I will have A*x<=1.

For example, M=N=2, I will have something like this: enter image description here

And, I will create a function test(x) which returns true or false according to x.

I would like to get some help from you and optimize my code.

Upvotes: 1

Views: 284

Answers (1)

rayryeng
rayryeng

Reputation: 104474

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);

This will check every element of first or second and see if the resulting sums after you do the above code that I just showed are all <= 1. If they all satisfy this constraint, we will have true. Else, we have false.

Please let me know if you need anything further.

Upvotes: 2

Related Questions