Every nth column from in matlab to R - r

I am trying to translate some matlab code to R.
This is the code:
v_i = vfull(:,i:ns:k*ns);
I think this means take every ns_th column from i to k*ns in the matrix v_i, right? If I am wrong, can someone please tell me what it means?
Regardless, how would one implement this in R?

You interpretation of what Matlab is doing is correct. Here is how to do the equivalent in R
v_i = vfull[ ,seq(i,(k*ns),ns)]

Related

Creating an array of iterated values

I have read through a lot of julia-posts, but haven't found anything concerning this very special problem.
We are iterating eigenvalues and corresponding eigenvectors via Power-Iteration.
The algorithm is working and calculating the approximate eigenvalue, but we are asked to save the values of our approximate values after each iteration in a seperate vector.
I tried:
collect(lambda_k)
in a while loop:
l_end = []
push!(l_end,lambda_k)
But it doesn't work, are you able to help me?
Greetings!
Mass_Matics

Maxima: equations: eliminate variable inside diff() expression

I have this code:
(%i3)depends([y,x],t)$
eqsp: [y=2*x,
v=diff(y,t,1)+y];
eliminate(eqsp,[y]);
(eqsp) [y=2*x,v='diff(y,t,1)+y]
(%o3) [-'diff(y,t,1)-2*x+v]
And this is a picture for better visualization:
PNG of code in wxMaxima
I was expecting that Maxima would perform a substitution of "y" in the second equation, and then differentiate to get "[-2*diff(x,t,1)-2*x+v]".
This is of course not the real problem (which has many more equations), it's just that I think I'm missing some concept here for Maxima to do what I want.
Thanks in advance for your comments. I'm a newbie in Stackoverflow and in Maxima, sorry if I made some mistake.

Dynamic hedge ratio in R

I was trying to convert the Chan code about Pairs Trading strategy with dynamic hedge ratio into R.
I have already done all work with a steady hedge, but if I want to replicate his "for" loop I'm in trouble. Here my code for that part
lookback=20
hedge=rep(NaN,length(stockY))
for (i in lookback:length(hedge)){
reg=lm(stockY[i-lookback +1:i]~stockX[i-lookback +1:i])
hedge[i]=reg[1]$coeff[2]
}
I tried many different attempts but my low level in R is pretty evident here. I'm not trying to use a "lapply" function but just a for loop. Hope someone can help me. Thanks
Ok It seems I did it. The following is my code:
lookback=20
hedge=data.frame(hedge=rep(0,length(stockY)))
for (i in lookback:length(stockY)){
reg=summary(lm(stockY[i-lookback +1:i]~stockX[i-lookback +1:i]))
hedge[i,1]=reg$coefficients[2,1]
}
So, now I'd know how to extract my residuals. I mean, I need to list all residuals from regression. Unfortunately if I write reg$residuals it returns my last 20 residuals from the last iteration of the loop. So, I tried to include another "res" vector like "hedge" but.....I can't extract my residuals. Please, can someone help me?

Sum up with R base

Sorry, that I didn't wrote the sum function in latex. I tried $$ but that didn't work...
I am a beginner in R and I want to sum up:
Sum from i=0 to n by 1.054^i
I reserached the sum() function in R. However, it seems to me that is only can just add elements and not include an index or something.
So my question is: Can I solve that with a simple sum function or do I have to use a for loop for example?
I really appreciate your answer!
UPDATE
Here is a link to my sum(sorry that I cannot post it, but I need more reputation :(link )
In R most operations are vectorized, it requires changing the mindset a bit from other languages and for this question the answer is rather than looping, you simply do the entire operation on your sequence of numbers "at once":
sum(1.054^(0:n))

R or MATLAB: permute a large sparse matrix into a block diagonal matrix

I have a large sparse matrix, and I want to permute its rows or columns to turn the original matrix into a block diagonal matrix. Anyone knows which functions in R or MATLAB can do this? Thanks a lot.
I'm not really set up to test this, but for a matrix m I would try:
p = symrcm(m);
block_m = m(p,p);
If that doesn't work, look through the other functions listed in help sparfun to see if any will help you out.
The seriation package in R has a number of tools for problems related to this one.
Not exactly sure if this is what you want, but in MATLAB this is what I have used in the past. Probably not the most elegant solution.
I go from sparse to full and then chop the thing into square blocks.
A=full(A);
Then:
blockedmatrix = mat2cell(A, (n*ones(1,size(A,1)/n)), ...
(n*ones(1,size(A,1)/n))); %found somewhere on internetz
This returns a cell, where each entry is of size nxn.
It's easy to extract the blocks of interest, manipulate them, and then restore them to a matrix with cell2mat.
Maybe a bit late to the game, but since there are available commands, here is a simple one. If you have a matrix H and the block diagonal form is needed, you can obtain it through the following lines (MATLAB):
[p,q] = dmperm(H);
H(p,q)
which is equivalent to Dulmage - Mendelsohn permutation.

Resources