How to add power of x from 0 to n-1

1 回表示 (過去 30 日間)
Karim Belkhiria
Karim Belkhiria 2015 年 11 月 5 日
コメント済み: Matt J 2015 年 11 月 5 日
I have a function vandermonde
function c = vandermonde(x, y)
V = [x.^0 x x.^2 x.^3]
c = V \ y;
end
it works good if I give a vector x with 4 components. But how could I make it general, even if I give a vector x with 5,6 or even 15 components? so that V = [x.^0 x x.^2 x.^3 x.^4 x.^5 .... x.^14] ? with n or something like that?

採用された回答

Matt J
Matt J 2015 年 11 月 5 日
V=bsxfun(@power,x(:),0:n-1)
  3 件のコメント
Karim Belkhiria
Karim Belkhiria 2015 年 11 月 5 日
thank you for your answer! It works! but we are not allowed to use bsxfun and @power. It is a homework, and we are beginners with matlab. it should be something really simple with the basics of MATLAB. Is there any other solution with "basic" commands?
Matt J
Matt J 2015 年 11 月 5 日
Yes, perhaps. But it is your homework...

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

その他の回答 (1 件)

Karim Belkhiria
Karim Belkhiria 2015 年 11 月 5 日
編集済み: Karim Belkhiria 2015 年 11 月 5 日
here is the answer that I searched for:
function c = vmonde(x, y)
n = length(x);
V = ones(n);
for j = 2:n
V(:,j) = x.*V(:,j-1);
end
c = V \ y;
disp(V)
end
  1 件のコメント
Matt J
Matt J 2015 年 11 月 5 日
I wonder if your teacher would also have accepted this
V=exp(log(x(:))*(0:n-1))

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by