0

I would like to translate a matlab function into R code. Here is the part of the code I am most confused about:

            obj = (Pv*v)'*double(ttv(Xhat,Pv*v,2))*(Pu*u);      
            ind = 1; iter = 1;
            while ind>thr & maxit>iter
                oldo = obj(end);
                uhat = Pu*double(ttv(Xhat,v,1))'*v;
                u = uhat/norm(uhat);
                [v,tmp] = eigs(Pv*double(ttv(Xhat,u,3))*Pv,1);
                obj = [obj (Pv*v)'*double(ttv(Xhat,Pv*v,2))*(Pu*u)];
                ind = abs((obj(end) - oldo)/obj(1));
                iter = iter + 1;
            end
            d = v'*double(ttv(Xhat,u,3))*v;

1) First question is with line 4 oldo = obj(end). Can someone explain to me this syntax? (I think that it will end the while loop?) This is also in line 9.

2) Line 7: [v,tmp] = eigs(Pv*double(ttv(Xhat,u,3))*Pv,1);. I do not understand what [v, tmp] is. "v" is a previously defined variable, but tmp is not. Can someone explain this data structure?

3) Line 8: obj = [obj (Pv*v)'*double(ttv(Xhat,Pv*v,2))*(Pu*u)];. I am confused about what operations are occuring within the brackets - e.g., there is a space but no operator between "obj" and "(Pv*v)...". Do the brackets act as some sort of operator here?

FYI, ttv() is a function from Tensor Toolbox. If anyone can help with any of these questions, that would help me greatly. I know this is sort of a vague/multi-part question, but I figured if someone could answer one of these, they could answer them all. Also, if you just explain what these lines of code do in matlab without translating them to R, that would be very helpful, too. The full code is available here: https://github.com/zhengwu/TensorNetwork_PCA/blob/master/hopca_popNet_new.m

Thank you for your help!

  • Sounds like you should check out a simple/introductory Matlab tutorial, https://www.mathworks.com/help/matlab/getting-started-with-matlab.html – David Jan 31 '20 at 00:03
  • Hi David, thanks for the comment. I do know pretty basic matlab, I am just in that awkward part where the code is past the intro stuff and things get difficult to google. For example, I am not sure how to look up my second question, where, generalizing, [x, y] = data. I have tried to look this up, but don't even know how to refer to this. – pigtowndandy Jan 31 '20 at 00:19
  • It's just multiple outputs from a function. – David Jan 31 '20 at 00:53
  • I think `[v,tmp] = eigs(...)` is *multiple assignment*, where the function call `eigs(...)` is returning a list/vector/array of length 2, and the first is stored in `v`, the second is stored in `tmp`. https://www.mathworks.com/matlabcentral/answers/16996-assign-multiple-variables. (Perhaps R's version: `l <- eigs(...); v <- l[1]; tmp <- l[-1]`.) In the case of this code, it appears that the value stored in `tmp` is not needed but still needs to be captured. – r2evans Jan 31 '20 at 05:45
  • Got it. Thanks for the help. I think what mainly threw me off is when I opened the code in my text editor the 'end' for the while loop and the 'end' in 'obj(end)' (which i now know specifies the last element of the vector) were the same color, so I thought it was somehow a way to terminate the while loop if obj was a certain value. It was all downhill from there. – pigtowndandy Jan 31 '20 at 22:07

0 Answers0