Vectorizing the creation of a matrix of successive powers

后端 未结 5 444
长情又很酷
长情又很酷 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条回答
  •  Happy的楠姐
    2020-12-07 01:28

    Since your matrices aren't that big, the most straight-forward way to do this would be to use MESHGRID and the element-wise power operator .^:

    [x,N] = meshgrid(1:100,0:10);
    x = x.^N;
    

    This creates an 11-by-100 matrix where each column i contains [i^0; i^1; i^2; ... i^10].

提交回复
热议问题