Random selection from datasource [duplicate] - matlab

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
randperm subset of random m-by-n matrix
When trying to randomly select 1000 rows from a dataset "fulldata" I get an error, im not very sure how its done in matlab but this is what I tryed:
data = datasample(fulldata,6,1000)
The fulldata is 490256x6 and I want to select 1000 random rows from this data. In matlab its throwing the error
??? Undefined function or method 'datasample' for input arguments
of type 'double'.
Error in ==> randomselection at 44
data = datasample(fulldata,6,1000)

You could instead to something like this to get it without relying on datasample:
pointsToPick = 1000; %# Numbers to pick
rVec = randperm(N); %# Random permutation of datapoint indices (N=490256 in your case)
randomSample = fulldata(rVec(1:pointsToPick),:); %# Random sample

Related

Storing iterative function outputs into matrices [duplicate]

This question already has an answer here:
Saving return values of function returning multiple variables in Matlab
(1 answer)
Closed 7 years ago.
I have an iterative function which gives me two vector outputs. How can I store these outputs into two separate matrices in matlab?
[A, B]=iterative_function(x,y)
the size of A and B varies in every loop.
If the sizes of the outputs vary, it's best to store them in a cell array:
A_cell = cell(1, num_iter);
B_cell = cell(1, num_iter);
for ii = 1:num_iter
...
[A_cell{ii}, B_cell{ii}] = iterative_function(x, y);
...
end
where num_iter is the number of iterations and ii is the loop variable.

Mysterious MATLAB error using ./ in a for loop [duplicate]

This question already has answers here:
Multiple loop variables Matlab
(3 answers)
Closed 7 years ago.
I need to make a for loop in MATLAB to divide each column in a matrix by a separate column vector. I only want to do this for a selection of the columns in the matrix, not all the columns.
This is what I'd like to do, where Indexes is a 19x1 vector of integers (not all consecutive numbers), big_matrix is 82x24, and other_column is 82x1:
matrix_to_fill = zeros(82,length(Indexes));
for x = Indexes
new_column = big_matrix(:,x)./other_column;
new_index = find(Indexes==x);
matrix_to_fill(:,new_index) = new_column;
end
When I run this I get the following error:
Error using ./
Matrix dimensions must agree.
I can run each iteration separately without getting errors, so I know that the matrix dimensions agree. What's more, if I type out the Indexes as a vector it works fine:
matrix_to_fill = zeros(82,length(Indexes));
for x = [1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,23]
new_column = big_matrix(:,x)./other_column;
new_index = find(Indexes==x);
matrix_to_fill(:,new_index) = new_column;
end
And I think the "x=Indexes" syntax is fine because I've tested that using just:
for x = Indexes
disp(x)
end
So I'm completely stumped. Any help would be much appreciated!
The problem is in your definition of the for loop. When you say that you think the "x=Indexes" syntax is correct you haven't been observant enough to see that it is not correct.
What you need is
for x = Indexes'
% Do your looping
end
Note the transpose in the above.
If you do
for x = Indexes
disp(x)
end
Then the loop is executed once, with x taking on the value of the whole vector.
If you do
for x = Indexes'
disp(x)
end
then x will take on the individual elements of the matrix and you'll have 19 scalars displayed, once each time through the loop.

How to store a series of vectors from a for loop matlab [duplicate]

This question already has an answer here:
Subscript indices must either be real positive integers or logicals?
(1 answer)
Closed 8 years ago.
I have a for loop which generates a vector. i want to store these vectors in a matrix.
normally i would do:
for r=1:100
vec=[x:y]+r;
mat(:,r)=vec
end
But this doesnt work, because i have something like:
dr=10/20
for r=1:dr:20
vec=[x;y]+r;
...
How would I store the vectors in a matrix now? Because i cant use r for the column indices, because the values of r arent integers most of the time.
Many options eg:
r=1:dr:20
for rr=1:length(r)
vec=[x;y]+r(rr);
mat(:,rr)=vec;
...
or
col = 1;
for r=1:dr:20
vec=[x;y]+r;
mat(:,col)=vec;
col = col + 1;
....
But whatever you choose you must preallocate mat before your for loop like this:
mat = zeros(length(x) + length(y), length(1:dr:20))
Pre-allocation is essential when using loops in Matlab or they will run very inefficiently.

How can I split a large column of data into multiple columns of data in MATLAB? [duplicate]

This question already has answers here:
How do I resize a matrix in MATLAB?
(3 answers)
Closed 8 years ago.
I have a large column of data (400,000+ data points) being imported from a text file. I need the column to be divided into multiple columns of length 7,000. It is already in order, so the first 7,000 data points would stay in the first column, the next 7,000 would go to the next column, and so forth...
You can reshape the data into a 2D matrix, but all columns must be the same length (7,000).
You can either select only complete columns:
x = rand(1,400000);
N = 7000;
cols = floor(length(x)/N);
y = reshape(x(1:N*cols), [N,cols]);
Alternatively, you can pad the final column (eg with zeros):
x = rand(1,400000);
N = 7000;
cols = ceil(length(x)/N);
y = zeros(N, cols);
y(1:length(x)) = x;
Use buffer command:
x = rand(400000,1);
[out,z]=buffer(x,7000);
Here out corresponds to the 2D matrix with 7000 elements in each column. If the length of the vector is not divisible by 7000, the last column of data which has length less than 7000 is stored in z.
Using reshape function can do this
A = reshape(B,7000,[]);

generating a random value between 0.001 and 0.0015 [duplicate]

This question already has answers here:
Matlab, matrix containing random numbers within specified range
(3 answers)
Closed 8 years ago.
I want to generate random values between 0.001 and 0.0015 such that each time I run a for loop, i get a new value.
e.g
value = random number between 0.001 and 0.0015;
for i = 1:10,
for value,
Calculate something...
end
end
Can any one tell me how to do that?
It's all written in the documentation of rand() function: http://www.mathworks.com/help/matlab/ref/rand.html
Example 1 Generate values from the uniform distribution on the
interval [a, b]:
r = a + (b-a).*rand(100,1);
So in your case a = 0.001 and b = 0.0015 and you can also change rand(100,1) to just rand(10,1) to give you 10 random values on the interval [a,b].
It's simple, you just do:
0.001+(rand()*(0.0015-0.001))
Here is how you should be doing it in my opinion:
myNumbers = rand(10,1)*0.0005+0.001;
for value = myNumbers;
%Calculate something
value
end
Note that the main improvement here is that you precalculate all random values at once.
Furthermore you can loop over them directly.