% Tensor Train with ALS, rank 3
for i1=0:1;for i2=0:1;
                A(i1+1,i2+1)=i1+2*i2;
            end;end;
T=tensor(A);
a0=[1;0;0]; a1=[0;1;2]; b0=a0;b1=a1;
c0=[A(1,1);A(1,2)]; c1=[A(2,1);A(2,2)];
d0=[A(1,1);A(2,1)]; d1=[A(1,2);A(2,2)];
for j=1:50;
    BB=[b0';b1'];
    a0=pinv(BB'*BB)*BB'*c0;
    a1=pinv(BB'*BB)*BB'*c1;
    AA=[a0';a1'];
    b0=pinv(AA'*AA)*AA'*d0;
    b1=pinv(AA'*AA)*AA'*d1;
end
SS(1,1)=a0'*b0; SS(1,2)=a0'*b1;
SS(2,1)=a1'*b0; SS(2,2)=a1'*b1;
norm(full(T)-SS)
