how to write a very concise expression for matrix power

I have a row vector, e.g. [1,2,3,4], and I want to put, say 3 different power [1;2;3] to create a matrix as follow:
[1,2,3,4]?^[1;2;3] = [1,2,3,4 ; 1,4,9,16 ; 1,8,27,64].
Anyone knows how to get it without using a loop. Many thanks!

 採用された回答

Matt J
Matt J 2016 年 10 月 12 日

1 投票

As of R2016b, you can just do this
[1;2;3] .^ [1,2,3,4]
Otherwise, use bsxfun()

4 件のコメント

Xin
Xin 2016 年 10 月 12 日
編集済み: Xin 2016 年 10 月 12 日
Ahh, I am using 2011, didn't know it works in 2016.
Thanks for reply. Is there any other way to let it work.
Matt J
Matt J 2016 年 10 月 12 日
編集済み: Matt J 2016 年 10 月 12 日
Upgrade!
Or, use bsxfun() as I suggested.
Xin
Xin 2016 年 10 月 12 日
Nice, bsxfun does work. Thank you so much!
dpb
dpb 2016 年 10 月 12 日
Then please ACCEPT the answer...

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2016 年 10 月 12 日

0 投票

>> bsxfun(@power,[1:4],[1:3].')
ans =
1 2 3 4
1 4 9 16
1 8 27 64
>>

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

タグ

質問済み:

Xin
2016 年 10 月 12 日

コメント済み:

dpb
2016 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by