Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


Curious
Posts:
2,057
Registered:
12/6/04


Re: Integrating bsxfun in a forloop
Posted:
Feb 23, 2014 5:02 PM


"Timothy Liang" <timothy.tliang@gmail.com> wrote in message <leb9qk$qn0$1@newscl01ah.mathworks.com>... > Hi, > > I am trying to integrate bsxfun into a loop... but it turned out to be unsuccessful... > > i=1:10000 > x=i/10000 > for q=0:20 > X=bsxfun(@power,x',q); > [Q,R]=qr(X) > ... > end > > What I want was that as q increase, the matrix would be > for q=0 > X=x1^0 > x2^0 > x3^0 > ... > > then for q=1 > X=x1^0 x1^1 > x2^0 x2^1 > x3^0 x3^1 > > for q=2 > X=x1^0 x1^1 x1^2 > x2^0 x2^1 x2^2 > x3^0 x3^1 x3^2 > and so on... > > However, the result of my codes was Xs are always 10000by1 column matrices, where the first matrix is x^0, second one x^1... > How do I fix this? Thanks in advance!!!!
You are overwriting X each time thru your for loop. You need to modify the above code to something like:
i=1:10000 x=i/10000 maxq = 21 X = zeros(maxq,length(x)) % preallocatefor efficiency for q=1:maxq X(q,:)=bsxfun(@power,x',(q1)); [Q,R]=qr(X) ... end



