Vectorizing the creation of a matrix of successive powers

后端 未结 5 441
长情又很酷
长情又很酷 2020-12-07 00:47

Let x=1:100 and N=1:10. I would like to create a matrix x^N so that the ith column contains the entries

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 01:24

    Not sure if it really fits your question.

    bsxfun(@power, cumsum(ones(100,10),2), cumsum(ones(100,10),1))
    

    EDIT: As pointed out by Adrien, my first attempt was not compliant with the OP question.

    xn = 100;
    N=10;
    solution = [ones(1,xn); bsxfun(@power, cumsum(ones(N,xn),2), cumsum(ones(N,xn),1))];
    

提交回复
热议问题