Vandermonde-like matrix

1 回表示 (過去 30 日間)
Mokrane Mahdi
Mokrane Mahdi 2022 年 3 月 3 日
コメント済み: Mokrane Mahdi 2022 年 3 月 3 日
For any given number x, what's the easiest way to generate the following square matrix without using any loop:
0 x^(-0.5) x^(-1.0) x^(-1.5) x^(-2.0)
x^(-0.5) x^(-1.0) x^(-1.5) x^(-2.0) x^(-2.5)
x^(-1.0) x^(-1.5) x^(-2.0) x^(-2.5) x^(-3.0)
x^(-1.5) x^(-2.0) x^(-2.5) x^(-3.0) x^(-3.5)
x^(-2.0) x^(-2.5) x^(-3.0) x^(-3.5) x^(-4.0)
Here, in this example, I set the size of the matrix to be 5, but it has to be generated for any integer n.
Notice the matrix does look like the Vandermonde matrix, hence the idea of using repmat and cumprod commands..
Thanks in advance !

採用された回答

KSSV
KSSV 2022 年 3 月 3 日
p = -(0:0.5:4) ; % power values
n = 5 ; % n value
ind1 = bsxfun(@plus, (1 : n), (0 : numel(p) - n).'); % make moving window indices
p = p(ind1) % power values
p = 5×5
0 -0.5000 -1.0000 -1.5000 -2.0000 -0.5000 -1.0000 -1.5000 -2.0000 -2.5000 -1.0000 -1.5000 -2.0000 -2.5000 -3.0000 -1.5000 -2.0000 -2.5000 -3.0000 -3.5000 -2.0000 -2.5000 -3.0000 -3.5000 -4.0000
x = rand ; % your x
V = x.^p % what you wanted
V = 5×5
1.0e+03 * 0.0010 0.0028 0.0077 0.0214 0.0595 0.0028 0.0077 0.0214 0.0595 0.1654 0.0077 0.0214 0.0595 0.1654 0.4594 0.0214 0.0595 0.1654 0.4594 1.2760 0.0595 0.1654 0.4594 1.2760 3.5443
  1 件のコメント
Mokrane Mahdi
Mokrane Mahdi 2022 年 3 月 3 日
Thank you !

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2022 年 3 月 3 日
The easiest way? Probably this line: (in R2016b or later)
x.^(((0:-1:1-n)' + (0:-1:1-n))/2)
If you want to use two lines of code, then it looks simpler yet.
N = (0:-1:1-n)/2;
x.^(N' + N)
Easrlier releases than R2016b would use bsxfun.
  1 件のコメント
Mokrane Mahdi
Mokrane Mahdi 2022 年 3 月 3 日
Oh, this is way more easy ! Thanks !

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by