How can I create a vector from data? - matlab

I used matlab to solve linear programming with the common [x, fval] = linprog(f,a,b) and I got the solution. My problem is I want to find the binary vector for the variables(x), for example, the values of (x) after I solve the linear problem were 13,0, 8,0,5,8,0,4,0,0 and I would like to obtain the vector(h) 1,0,1,0,1,1,0,1,0,0 which is represents the binary vector for x. I mean when the value of x greater than 0 we put in h 1 and when the value of x less than or equal 0 put 0 in the vector h?
Thanks.

What about
binvect=x>0;
In Matlab it is as easy as doing that, he will do give you a vector of all the x that fill the condition (>0)

Related

solving of zero indexing issue in matlab

suppose that we have some vector y in matlab and let us suppose that there is such linear relationship between values of y
of course first vector can be easily implemented by
y(L:N-1), but related to matrix, we dont have index y[0] in matlab, so how can i solve this issue? there is another picture
should i use index y1 and instead of index y(l-1),start from y(l)?
Your equation is zero-based, while Matlab is one-based in terms of array indexing. Therefore, the best solution is to add 1 every index in the equation: the vector on the left hand side runs from L+1:N, instead of L:N-1, and the elements in the matrix from 1 to L or N-L (whichever is larger), instead of 0 to L-1 or N-L-1.

For what value of k are these 3 vectors linearly dependent

So I have these three vectors:
And I have to find out for what value of k these three vectors are linearly dependent. I have tried using rref and linsolve with syms for this but that did not work out. I'm relatively new to MatLab and matrices so please keep that in mind.
I know in order to check if vectors are linearly dependent that c1...cn have to be non-zero.
I also want to know how you can use variables in general when solving these types of equations in MatLab.
A set of vectors (at least if you have n vectors in n dimensions) is linearly dependent if the matrix constructed from them is singular, i.e. if its determinant is 0. If you have the Symbolic Math Toolbox, you can construct a symbolic matrix:
syms k;
M = [1 k 0; -1 1 2; 0 0 3];
det(M)
This will tell you that det(M)==3*k+3, which you can solve by hand. But generally, you can ask matlab to solve it:
solve(det(M)==0,k);
which will tell you the answer is -1. So unless k==-1, these vectors are linearly independent (i.e. they comprise a basis of the Euclidean space R^3).
Update: If you don't have the Symbolic Math Toolbox, you could still try to find a numerical solution. First define a function
detfun=#(k) det([1 k 0; -1 1 2; 0 0 3]);
that for any value of k will give you the determinant of your matrix, for instance detfun(3) gives 12. Then you can use fsolve to find a numerical solution to the equation detfun(k)==0, by calling
fsolve(detfun,0)
in which the second argument, 0, refers to the starting point of the search performed by fsolve. This will tell you that the answer is k==-1, but a single call to fsolve will only give you a single solution. If your function has multiple roots, you have to play around with the starting points to find more of them. In this case, you can know that your function (i.e. det(M(k)) is linear in k, so it has a unique root.

From a vector to a shorter vector of averages in Matlab

Let I have a vector x in Matlab of size 1000. I want to produce a vector y of length 10 where each component contains the average of 100 records from the vector x.
My attempt is quite simple
for i=1:10
y(i) = mean(x((1:100)+(i-1)*100));
end
I wonder if there is a build in command or more elegant solution for this.
You can use the reshape-function to generate a 2d-array and then calculate the mean over the correct dimension.
This works as replacement for your loop:
y = mean(reshape(x,[100,10]),1);
Note: It does not matter if x is a column-vector or a row-vector.

Matlab: Binary Linear Programming

I am trying to solve some equations on Matlab using Binary Integer Programming.
I have 3 sets of equations:
Ma.X=1
Mp.X<=1
Mr.X<=m*
Where, Ma is a known matrix with size 5*12
X is unknown set with size 12*1
Also Mp is known matrix with size 5*12
and Mr is a known matrix with size 4*12.
1 in the equations is an unity matrix with size 5*1 in both sets(1&2)
m* is a given known matrix with size 4*1
I'm trying to use command bintprog but how to put 1 equality and 2 inequalities
to get values of X. Also I don't have Function f to insert, I just have set of equations. Given that X unknown values with values 1 or 0.
I tried this command bintprog([],Ma,One51,Mp,One51)
but it gives me The problem is infeasible. with zeros answer matrix.
Please help me to solve this on Matlab
The correct syntax for bintprog is X = bintprog(f,A,b,Aeq,beq).
If you don't have f (which means you just want any feasible point), you can set it to []. However, for the others, your syntax is slightly wrong.
I am assuming that the + in your constraints is actually a * since otherwise, the matrix algebra doesn't quite make sense.
Try this:
X = bintprog([],[Mp;Mr],[ones(5,1);mstar],Ma,ones(5,1))
If even then it tells you that the problem is infeasible; it might as well be true that there are no Xs that can satisfy all your constraints.

Adding a random number to the matrix MATLAB

I want to generate 100x1 matrix with 3 numbers -1,1 and 0. I want to be able to control how much of 1's and -1's are assigned. I tried using
Y = rand(10,1)<0.1
but this only gives me 0's an 1's. But I am able to control the number of 1's in the matrix . Is there a similar type of function that I can use for adding and controlling the number of -1 and 1's along with the default 0. Sorry I am new matlab env.
Thanks
Start by initializing your array:
x = [-1*ones(30,1); zeros(25,1);ones(45,1)];
then use matlab's wonderful indexing with randperm:
y= x(randperm(100));
plot (y, 'o')