0

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.

Clopen
  • 37
  • 8
  • 1
    What do you mean by `N(x,y)` format? Do you want a 3D matrix? Do you want to unroll the `x` and `y` matrices so that each sample is a pair of `(x,y)` values? It's unclear what it is you want. – rayryeng Sep 20 '15 at 02:38
  • Yes. i want to list the samples as pairs of (x,y) values. – Clopen Sep 20 '15 at 22:44

2 Answers2

0

You can use mvnrnd(mu_vector, sigma_matrix)

mu = [-5;0];
Sigma = [1,0;0,1];
n = 1000;
X = mvnrnd(mu, Sigma, n);
Odedy
  • 80
  • 2
  • 8
  • I don't think that's what the OP wants IMHO. Read the description of what they want carefully. You'll see that it's quite ambiguous. – rayryeng Sep 20 '15 at 17:14
0

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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • This is exactly what I'm looking for. Do you know if there is also a way to add in parenthesis and commas so the pairs are printed "(x,y)"? – Clopen Sep 20 '15 at 23:13
  • Ah so you actually want this to be a string array? Yes it's possible...but let me know what format you want this in. – rayryeng Sep 20 '15 at 23:27
  • 3 digits of precision and I would like the format to be "x , y". CSV format. I would like to print the x values separated from the y values by a comma. – Clopen Sep 20 '15 at 23:41
  • Do you want to write this to a file then? It would be better if you did it that way if you want this to be a CSV file or format. – rayryeng Sep 21 '15 at 00:12
  • Yes. I need to write this to a CSV file. – Clopen Sep 21 '15 at 00:20
  • If you want this as a CSV file, it wouldn't make sense to have parentheses...did you just want it as `x,y` where each line is one pair? – rayryeng Sep 21 '15 at 00:22
  • precisely. Each line should read x,y,Class_A where each x comes from the values generated by normrnd for x, y should come from values generated by normrnd y, and the ",Class_A" should be concatenated onto the end. – Clopen Sep 21 '15 at 00:39
  • @Clopen - Your specifications keep changing. It's getting annoying... but I'll write an answer. – rayryeng Sep 21 '15 at 00:43
  • @Clopen - Done. If you have another question, please consider asking another one. It's usually bad form to ask further questions on a question that has already been solved / accepted. – rayryeng Sep 21 '15 at 00:50