I have two time series x and y which roughly cover the same period of time. The data is in daily form however there are some days that have data in one dataset but no data in the other. I wish to use matlab to create two data-sets of equal size with matching dates. Essentially I wish to remove the days that don't have data in both x and y. Is there a simple way to do this? Thanks.
You could use an inner join see help join if you are able to convert your timeseries into datasets. If not you could use the ismember function, but this time you should do it only on the dates.
Something like this will work:
a = {'2015-01-01', '2015-02-02', '2015-03-03'};
b = {'2015-01-01', '2015-03-03', '2015-04-04'};
newA = a(ismember(a,b));
newB = b(ismember(b,a));
I'm struggling with one of my matlab assignments. I want to create 10 different models. Each of them is based on the same original array of dimensions 1x100 m_est. Then with for loop I am choosing 5 random values from the original model and want to add the same random value to each of them. The cycle repeats 10 times chosing different values each time and adding different random number. Here is a part of my code:
steps=10;
for s=1:steps
for i=1:1:5
rl(s,i)=m_est(randi(numel(m_est)));
rl_nr(s,i)=find(rl(s,i)==m_est);
a=-1;
b=1;
r(s)=(b-a)*rand(1,1)+a;
end
pert_layers(s,:)=rl(s,:)+r(s);
M=repmat(m_est',s,1);
end
for k=steps
for m=1:1:5
M_pert=M;
M_pert(1:k,rl_nr(k,1:m))=pert_layers(1:k,1:m);
end
end
In matrix M I am storing 10 initial models and want to replace the random numbers with indices from rl_nr matrix into those stored in pert_layers matrix. However, the last loop responsible for assigning values from pert_layers to rl_nr indices does not work properly.
Does anyone know how to solve this?
Best regards
Your code uses a lot of loops and in this particular circumstance, it's quite inefficient. It's better if you actually vectorize your code. As such, let me go through your problem description one point at a time and let's code up each part (if applicable):
I want to create 10 different models. Each of them is based on the same original array of dimensions 1x100 m_est.
I'm interpreting this as you having an array m_est of 100 elements, and with this array, you wish to create 10 different "models", where each model is 5 elements sampled from m_est. rl will store these values from m_est while rl_nr will store the indices / locations of where these values originated from. Also, for each model, you wish to add a random value to every element that is part of this model.
Then with for loop I am choosing 5 random values from the original model and want to add the same random value to each of them.
Instead of doing this with a for loop, generate all of your random indices in one go. Since you have 10 steps, and we wish to sample 5 points per step, you have 10*5 = 50 points in total. As such, why don't you use randperm instead? randperm is exactly what you're looking for, and we can use this to generate unique random indices so that we can ultimately use this to sample from m_est. randperm generates a vector from 1 to N but returns a random permutation of these elements. This way, you only get numbers enumerated from 1 to N exactly once and we will ensure no repeats. As such, simply use randperm to generate 50 elements, then reshape this array into a matrix of size 10 x 5, where the number of rows tells you the number of steps you want, while the number of columns is the total number of points per model. Therefore, do something like this:
num_steps = 10;
num_points_model = 5;
ind = randperm(numel(m_est));
ind = ind(1:num_steps*num_points_model);
rl_nr = reshape(ind, num_steps, num_points_model);
rl = m_est(rl_nr);
The first two lines are pretty straight forward. We are just declaring the total number of steps you want to take, as well as the total number of points per model. Next, what we will do is generate a random permutation of length 100, where elements are enumerated from 1 to 100, but they are in random order. You'll notice that this random vector uses only a value within the range of 1 to 100 exactly once. Because you only want to get 50 points in total, simply subset this vector so that we only get the first 50 random indices generated from randperm. These random indices get stored in ind.
Next, we simply reshape ind into a 10 x 5 matrix to get rl_nr. rl_nr will contain those indices that will be used to select those entries from m_est which is of size 10 x 5. Finally, rl will be a matrix of the same size as rl_nr, but it will contain the actual random values sampled from m_est. These random values correspond to those indices generated from rl_nr.
Now, the final step would be to add the same random number to each model. You can certainly use repmat to replicate a random column vector of 10 elements long, and duplicate them 5 times so that we have 5 columns then add this matrix together with rl.... so something like:
a = -1;
b = 1;
r = (b-a)*rand(num_steps, 1) + a;
r = repmat(r, 1, num_points_model);
M_pert = rl + r;
Now M_pert is the final result you want, where we take each model that is stored in rl and add the same random value to each corresponding model in the matrix. However, if I can suggest something more efficient, I would suggest you use bsxfun instead, which does this replication under the hood. Essentially, the above code would be replaced with:
a = -1;
b = 1;
r = (b-a)*rand(num_steps, 1) + a;
M_pert = bsxfun(#plus, rl, r);
Much easier to read, and less code. M_pert will contain your models in each row, with the same random value added to each particular model.
The cycle repeats 10 times chosing different values each time and adding different random number.
Already done in the above steps.
I hope you didn't find it an imposition to completely rewrite your code so that it's more vectorized, but I think this was a great opportunity to show you some of the more advanced functions that MATLAB has to offer, as well as more efficient ways to generate your random values, rather than looping and generating the values one at a time.
Hopefully this will get you started. Good luck!
I'm struggling here as it's my first attempt with Matlab...
I have data that looks like this:
The first row has stockID number and the 60 rows in each column contain the stock's returns.
I am trying to calculate the variance for each stock as well as a covariance matrix in Matlab. I am stuck because I do not know how to identify each column as its StockID. Should each column be its own variable? If so, how would I do this automatically as I have about 1,000 stocks...? Is there then a way to create a cov. matrix for each stock without manually entering in each variable, i.e. not do this: cov(10801, 12032, 13439, .....) ?
Thanks so much for the help!
You should be able to find the covariance by passing the second through 60th rows of your data into the cov function (covariance_matrix = cov(data(2:end,:))), as per this documentation. Hope that helps!
I'd like to calculate the sum of a columns in a Matrix in Matlab and assign the value to another Matrix without a for loop (as I will be needed to do lots of these with little variations in the sum formula and writing for-loops for each of these would be silly).
Here is what I have so far:
finalmatrix= [
symsum((sample1Prime(i)-sample1(i))^2, i, 1, 10);
]
Note this would be one index of the final matrix.
I keep getting an error saying symsum is invalid for arguments of type double.. And a quick Google search tells me that it cannot be used for non-symbolic expressions. But no further solutions are given that meet my requirements.
Essentially what I'm trying to calculate is:
The sum from i = 1 to 10 of sample1Prime(i)-sample1(i))^2 where sample1Prime and sample1 are 10x1 Matrices.
Anyone have any ideas?
Thanks.
If you need the cumulative sum:
cumsum(sample1Prime-sample1.^2)
If you only need the final sum:
sum(sample1Prime-sample1.^2)
How can I compute the summation of an interval. I will use Matlab's code for eg.
data=[1;2;3;4;5;6;7;8;9;10;11;12]
I would like to perform this summation.
sum(1)=data(1)+data(2)+data(3)
sum(2)=data(4)+data(5)+data(6)
sum(3)=data(7)+(data(8)+data(9)
sum(4)=data(10)+data(11)+data(12)
How can I get about this? (Using for loop)
No for loop needed, if indeed this interval is constant like in your example:
Ans=sum(reshape(data,3,[]))
note that I reshape the vector data to a matrix that has the right number of columns, so the value 3 relates to the interval size you wanted...