Error creating matrix using a integer and vector in Matlab

1 回表示 (過去 30 日間)
Syed Masoom
Syed Masoom 2020 年 9 月 15 日
編集済み: Steven Lord 2020 年 9 月 16 日
I'm having trouble understanding why I can''t declare a matrix by using the following declaration A = [1 x x.^2 x.^3]; where x is from a column vector of 1x50 double

採用された回答

madhan ravi
madhan ravi 2020 年 9 月 15 日
編集済み: madhan ravi 2020 年 9 月 15 日
A = x(:) .^ (0:3)
  6 件のコメント
madhan ravi
madhan ravi 2020 年 9 月 15 日
A = x(:) .^ (0:3)
Syed Masoom
Syed Masoom 2020 年 9 月 15 日
thank you, could you post that as your answer so i can accept it?

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 9 月 16 日
編集済み: Steven Lord 2020 年 9 月 16 日
madhan ravi has told you how to do this but I want to explain why your original approach didn't work.
If x is a column vector with 50 elements it has 50 rows (if it were a 1x50 array it would be a row vector.) The number 1 has 1 row. You can't horizontally concatenate an array with one row and an array with 50 rows and have them form a rectangular array as you can see here:
bar([1 2], [1 50])
You could make that 1 an array with the same number of rows as x:
x = (1:10).';
A1 = [ones(size(x)), x, x.^2, x.^3];
Though I'd probably reverse the order of the columns to match the convention of functions like polyfit and polyval.
A2 = x.^(3:-1:0)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by