Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a vector in MATLAB with a pattern

How do I create a vector like this:

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

Is it possible to create aNew without a loop?

like image 874
user558213 Avatar asked Dec 08 '25 20:12

user558213


1 Answers

So you want different powers of a, all strung out into a vector? I would create an array, where each column of the array is a different power of a. Then string it out into a vector. Something like this...

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

This does what you want, in a simple, efficient way. bsxfun is a more efficient way of writing the expansion than are other methods, such as repmat, ndgrid and meshgrid.

The code I wrote does assume that a is a column vector, as you have constructed it.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!