I am trying to generate a 2D data set with the following parameters:
x= N(-5,1) y= N(0,1) n= 1000
Where N(mean, std dev) and n = number of samples.
I tried:
x = normrnd(-5, 1, [100,10])
y = normrnd(0,1,[100,10])
to generate a 100 x 10 array with the appropriate values. I now need to find a way to output the values from these two arrays into an N(x,y) format that can be analyzed by Weka. Any suggestions on how to do this would be appreciated.
Given your comments, you want to generate a N x 2 matrix where each row is a pair of values that both come from different normal distributions.
You can either generate the 2D matrices of each separately and unroll them into single vectors and concatenate them both.... or the simplest way is to just generate 100 x 10 = 1000 elements in a 1D vector from each distribution and concatenate these together.
Method #1 - 2D matrix unrolling
x = normrnd(-5, 1, [100,10]);
y = normrnd(0, 1, [100,10]);
N = [x(:) y(:)];
Method #2 - 1D vector concatenation
x = normrnd(-5, 1, [1000,1]); %// Change
y = normrnd(0, 1, [1000,1]); %// Change
N = [x y];
If you wish to write this to a CSV file, where you have a pair of x,y values separated by a comma and you have Class_A at the end, a call to fopen to open up a file for writing, fwrite to write our stuff to the file and fclose to finally close the file is needed. You also require that the digits are 3 digits of precision. Something like this comes to mind:
f = fopen('numbers.csv', 'w'); %// Open up the file
fprintf(f,'%.3f,%.3f,Class_A\n', N.'); %'// Write the data
fclose(f); %// Close the file
It's important to look at the second statement carefully. Note that I'm writing the transpose of N because MATLAB writes values in column-major order. This means that if you want the rows to be written to the file, you have to transpose the matrix to do that. numbers.csv is what the file is called when it is written. If you examine this file now, you'll see that it's in the form of x,y,Class_A where x,y is a pair of values from both normal distributions.
You can use mvnrnd(mu_vector, sigma_matrix)
mu = [-5;0];
Sigma = [1,0;0,1];
n = 1000;
X = mvnrnd(mu, Sigma, n);
Related
I need to generate all square matrices of order n with given properties.
Matrices are symmetric.
Entries are 0 and 1.
Diagonal elements are zeros.
I am using Matlab2012b. Can you help me with the code?
I was trying to write it down. It needs a long sequences of for loops. Any simpler technique?
Try this:
N = 4; %// matrix size
M = (N^2-N)/2; %// number of values to fill in each matrix
P = 2^M; %// number of matrices
x = dec2bin(0:P-1)-'0'; %// each row contains the values of a matrix, "packed" in a vector
result = NaN(N,N,P); %// preallocate
for k = 1:P
result(:,:,k) = squareform(x(k,:)); %// unpack values
end
The matrices are result(:,:,1), result(:,:,2) etc.
I have an Nx2 matrix of data points where each row is a data point. I also have an NxK matrix of indices of the K nearest neighbours from the knnsearch function. I am trying to create a matrix that contains in each row the data point followed by the K neighbouring data points, i.e. for K = 2 we would have something like [data1, neighbour1, neighbour2] for each row.
I have been messing round with loops and attempting to index with matrices but to no avail, the fact that each datapoint is 1x2 is confusing me.
My ultimate aim is to calculate gradients to train an RBF network in a similar manner to:
D = (x_dist - y_dist)./(y_dist+(y_dist==0));
temp = y';
neg_gradient = -2.*sum(kron(D, ones(1,2)) .* ...
(repmat(y, 1, ndata) - repmat((temp(:))', ndata, 1)), 1);
neg_gradient = (reshape(neg_gradient, net.nout, ndata))';
You could use something along those lines:
K = 2;
nearest = knnsearch(data, data, 'K', K+1);%// Gets point itself and K nearest ones
mat = reshape(data(nearest.',:).',[],N).'; %// Extracts the coordinates
We generate data(nearest.',:) to get a 3*N-by-2 matrix, where every 3 consecutive rows are the points that correspond to each other. We transpose this to get the xy-coordinates into the same column. (MATLAB is column major, i.e. values in a column are stored consecutively). Then we reshape the data, so every column contains the xy-coordinates of the rows of nearest. So we only need to transpose once more in the end.
I am a beginner in matlab and I have a particular z matrix of size m×1 with values 0,1,3,5,2 etc..with above values repeating. Now I have 4 other column matrix x1,x2,x3 and y and I want to do regression.
I have used lm = LinearModel.fit(x,y,'linear') specifying columns.Now I want to do regression only for values in matrix x1,x2,x3 and y for those corresponding to z matrix with value of 1 and neglect the other rows.How do I do it?
That's very simple. I'm going to assume that your matrix of predictor variables and outputs are also of size m (number of samples). All you have to do is find the locations within z that are 1, subset your 3 column matrix of x1,x2,x3 and y, then use LinearModel.fit to fit your data. Assuming your matrix of predictors is stored in X, and your outputs are stored in y, you would do this:
ind = z == 1;
xOut = X(ind,:);
yOut = y(ind);
lm1 = LinearModel.fit(xOut, yOut, 'linear');
BTW, these are very simple subsetting operations in MATLAB. Suggest you read a tutorial before asking any further questions here.
Suppose I have 4 letters and I want to arrange them in 3 places (repetition allowed), so I would have 43=64 possible permutations. How can I compute and print them?
Simplifying Amro's answer, you could use this:
%// Sample data
x = 'ABCD'; %// Set of possible letters
K = 3; %// Length of each permutation
%// Create all possible permutations (with repetition) of letters stored in x
C = cell(K, 1); %// Preallocate a cell array
[C{:}] = ndgrid(x); %// Create K grids of values
y = cellfun(#(x){x(:)}, C); %// Convert grids to column vectors
y = [y{:}]; %// Obtain all permutations
Matrix y should store the permutations you're after.
How about the function N_PERMUTE_K from the File Exchange?
An intuitive one-liner:
unique(nchoosek(repmat('ABCD', 1,4), 3), 'rows')
Although nice-looking, it's slow and inefficient. Don't use it for large data sets.
Pseudocode solution:
Generate the (base ten) numbers 0 to 63.
Change them to base 4, which only has the digits 0, 1, 2, and 3.
Convert numbers to letters.
The actual Matlab code is left as an exercise for the student.
Simply put I have an N x M matrix and I would like to obtain a 256 bin histogram for each column of the matrix. I know how to do this with a for loop, but I need to do it in matrix notation to save valuable computation time.
Also, I would like to use imhist rather than hist.
For loop method:
data = randint(100,100,10);
for n = 1:100
k(:,n) = imhist(data(n,:));
end
hist operates on the column of the input matrix by default. So
>> k = hist( data, 0:255 );
should do the trick for you.