tags:

views:

67

answers:

2

Problem statement:- I want to create 50 instances of a vector(1 Dimension) which contains random real numbers(float). The array size(1 dimension) will be say 30 thousand.How do i proceed so that the overhead is minimum or complexity is minimum?

+4  A: 

Create a 50x30k matrix of random values

values = rand(50, 30000)

then each row will be a 30k-values random vector

 aVector = values(3,:)  % Row 3, all columns
Mau
Let me make myself clear.say the vector is vect1,vect2.....,vect50 each containing 30 thousand real numbers/float.These will be substituted in an equation say x1=sigma*y+vect1(i)*beta where sigma and beta are constants. Dont i need a loop to get 30,000 values of x?something like this..for i=1:50for j=1: 300000x(j)=sigma*y+vect(i)(j)*beta;endend
SKM
No, you can write vectorized code that uses the big matrix (and runs faster). See: http://www.mathworks.com/support/tech-notes/1100/1109.html#operators
Mau
@SKM: see my update.
Gacek
@Mau.Would you kindly explain the implication of 3 in values(3,:) ?Thank you
SKM
@SKM When you index a matrix in matlab with m(i, j) you get the element at row i, column j. If you substitute : for one of the indices you get either all rows or all columns (like in the example).
Mau
This im aware but why did you choose third row?just for example or does it have something specific meaning?
SKM
Just an example.
Mau
+3  A: 
N = 30000; %// length of your vectors
I = 50; %// number of instances
v = rand(I, N); 

In the example above you will generate a matrix, in which every row is a single vector. Random numbers are generated with uniform distribution (for Gaussian distribution, use randn).

If you need to create every instance separately use a loop:

for i=1:I
    v = rand(1, N);
    %// do something with v
end

But I wouldn't use that if I were you (it is slower and IMHO less clear).

EDIT:
Regarding your question under Mau's answer:
No, you don't need to index it on your own. Let Matlab do it for you.
For example, if you need to repeat following operation 50 times:

 x1 = sigma*y + beta * vect1;
 ...
 x50 = sigma*y + beta * vect50;

assuming y is a vector of size 1x30000, you can compute it in one line:

X = sigma*repmat(y, 50, 1) + beta * rand(50, 30000);

explanation: every row is a single vector you wanted to compute:

X(1,:) = x1;
...
X(50,:) = x50;

repmat(y,50,1) repeats your y vector 50 times in first dimension (rows)

Gacek
@Gacek,is this equivalent to for i=1:30000 for j=1:50v(i)(j)=rand();% then i use the first element of v for some computationx=sigma*y+v*beta;endendEventhough this is crude.
SKM
@SKM: nope, you misunderstood me. example with `for` loop was given to show you second way of generating such vectors. See my update, I hope it clearly shows the solution of your problem. I know it could be somehow difficult at the beginning, but try to forget about the loops when using Matlab.
Gacek
@Gacek,thank you for your time and effort.
SKM
Always glad to help
Gacek
@Gacek.....would you be kind enough to tell me how to convert your answer to an m file so that the above code becomes a function which will be called based on varying the variables I and N in order to plot the x's I number of times?
SKM
@SKM: well, to plot these 50 vectors you don't need to index them one by one as well. Just use `plot(X')` (big `X` - the matrix; `'` means transposition of the matrix). To create m-file containing your function, see this documentation page: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/function.html
Gacek
Thank you once again
SKM

related questions