Creating a vector in MATLAB with a template

How to create such a vector:

a = [a_1;a_2;...,a_n]; 
aNew = [a;a.^2;a.^3;...;a.^T].

Is it possible to create anNew without a loop?

+3
source share
2 answers

So, do you need different degrees of a, all drawn into a vector? I would create an array in which each column of the array is different from a. Then insert it into the vector. Something like that...

aNew = bsxfun(@power,a,1:T);
aNew = aNew(:);

It does what you want in a simple, effective way. bsxfun is a more efficient way of writing an extension than other methods like repmat, ndgrid and meshgrid.

The code I wrote assumes that a is the column vector as you built it.

+7
source

The idea is to use meshgridto create two size arrays n x T:

[n_mesh, t_mesh] = meshgrid(a, 1:T);

n_mesh - , a, t_mesh - , 1:T.

, , :

aNew = n_mesh .^ t_mesh;
+2

Source: https://habr.com/ru/post/1782764/


All Articles