Columnwise power operation in matrix

Hi everyone (first post here)!
Say I have a vector x = [1;2;3].
I would like to construct a matrix X, where every column is the vector x raised to the power of the column number:
1 1 1
X = 2 4 8
3 9 27
With a for-loop, this is easily done:
for i=1:3
X(:,i) = x.^i;
end
But could this be done with vector operation instead of using a for loop?

 채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 25일

1 개 추천

bsxfun( @power, x, 1:3 )

댓글 수: 2

Alexander
Alexander 2013년 3월 25일
Wow, thank's very much, I appreciate it!
Matt J
Matt J 2013년 3월 25일
But don't use this for fitting polynomials! Use POLYFIT instead.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by