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')
Related
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.
Lets say I have a matrix of x = [1;2;3;4;5;6;7;8;9;10;11]
I need to find the three middle numbers in that matrix (without counting or hard-coding) and assign it to a variable say y
So the y will be assigned the three middle elements in any data set.
How would I accomplish this?
To select the three entries in the middle of vector x, you can use
y = x(ceil(end/2)+[-1 0 1]);
More about this use of end can be found here.
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)
I am trying to add a number of vectors in a Matrix where each row represents a vector, but it gives me "Subscripted assignment dimension mismatch." error. The main problem is that each vector has a different size. I tried to add zeros at the end of the short vectors but I couldn't do it. Any Help.
Example:
%signal is a vector of data.
[x(1,:),y(1,:)] = findpeaks(signal1);
[x(2,:),y(2,:)] = findpeaks(signal2); %error as the peaks count in signal 2 is not the same as in signal 1.
OK, given two vectors of unequal length,
A=rand(1,10)
B=rand(1,5)
the proper way to deal with this is to use a cell array
D={A;B}
And then you can get whatever elements you want like this, for example:
D{1}(1:3) %// A(1:3)
If you don't want to use cells, you can add rows using this little function that adds row vector M to matrix F
addRow=#(F,M) [F NaN(size(F,1),size(M,2)-size(F,2));M NaN(1,size(F,2)-size(M,2))]
you would use it like this:
F=A
F=addRow(F,B)
I am working with largish binary matrices, at the moment up to 100x100.
Lets say I am working with 30x30 binary matrices. Then there are a total of 2^(30x30) binary matrices. I want to select a binary matrix at random, where each of the 2^(30x30) matrices has the same probability of being selected.
My solution attempt was to pick a number between 1 and 2^(30x30) using the function randi(n) with n = 2^(30x30) and then converting the result to the appropriate binary matrix. The problem I ran into was that randi(n) does not take values for n larger than 2^54. Matlab in general does not seem to like very large numbers.
Any suggestions?
If each matrix of booleans has equal probability, then the elements of the matrix each have equal probability of 0 and 1. You can just fill a matrix of the appropriate size with n² uniform random booleans.
I don't have MATLAB handy, but in Octave you'd do something like unidrnd(2, n, n) - 1.
You can use randint in the range [0 1]:
matrix=randint(30,30,[0 1]);
You can also use rand and threshold the resulting matrix:
matrix=rand(30,30);
matrix=round(matrix);
EDIT: just realized it also works with randi with the following syntax:
matrix=randi([0 1],30,30);