Creating Matrix with a loop in Matlab -


i want create matrix of following form

    y = [1 x x.^2 x.^3 x.^4 x.^5 ... x.^100] 

let x column vector. or more variants such

    y = [1 x1 x2 x3 (x1).^2 (x2).^2 (x3).^2 (x1.x2) (x2.x3) (x3.x1)] 

let x1,x2 , x3 column vectors let consider first one. tried using like

    y = [1 : x : x.^100] 

but didn't work because means take y = [1 x 2.*x 3.*x ... x.^100] ? (ie values between 1 x.^100 difference x) so, cannot used generate such matrix. please consider x = [1; 2; 3; 4]; , suggest way generate matrix

    y = [1 1 1 1 1;          1 2 4 8 16;          1 3 9 27 81;          1 4 16 64 256]; 

without manually having write

    y = [ones(size(x,1)) x x.^2 x.^3 x.^4] 

use bsxfun technique -

n = 5; %// number of columns needed in output x = [1; 2; 3; 4]; %// or [1:4]' y = bsxfun(@power,x,[0:n-1]) 

output -

y =      1     1     1     1     1      1     2     4     8    16      1     3     9    27    81      1     4    16    64   256 

if have x = [1 2; 3 4; 5 6] , want y = [1 1 1 2 4; 1 3 9 4 16; 1 5 25 6 36] i.e. y = [ 1 x1 x1.^2 x2 x2.^2 ] column vectors x1, x2 ..., can use one-liner -

[ones(size(x,1),1) reshape(bsxfun(@power,permute(x,[1 3 2]),1:2),size(x,1),[])] 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -