Error using ^ (line 51) Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.

41 ビュー (過去 30 日間)
Please help me to fix the problem mentioned as Error-1 and Error-2. I will be very thankful.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Error -1
Error using ^ (line 51)
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To
perform elementwise matrix powers, use '.^'.
Error in PE_HT (line 125)
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx+c4*x_rec(i-1)*hx+c5*x_rec(i-1)^2+c6*hx^2+c7*x_rec(i-1)^2*hx^2;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Error -2 [When I replaced ^ with .^ then i get the error given below]
Unable to perform assignment because the left and right sides have a different number of elements.
Error in PE_HT (line 125)
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx+c4*x_rec(i-1)*hx+c5*x_rec(i-1).^2+c6*hx.^2+c7*x_rec(i-1).^2*hx.^2;

採用された回答

James Tursa
James Tursa 2020 年 9 月 29 日
編集済み: James Tursa 2020 年 9 月 29 日
This is often caused by using a matrix or vector in an equation when you thought you were using a scalar. E.g., take these lines:
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx+c4*x_rec(i-1)*hx+c5*x_rec(i-1)^2+c6*hx^2+c7*x_rec(i-1)^2*hx^2;
hx(i)=d1+d2*x_rec(i-1)+d3*hx+d4*x_rec(i-1)*hx+d5*x_rec(i-1)^2+d6*hx^2+d7*x_rec(i-1)^2*hx^2;
It is very suspicious to me that you have hx(i) on the left hand side, indicating that hx is a vector, and yet you have hx^2 on the right hand side, indicating that hx is a scalar. These don't seem to match. Why wouldn't you be using hx(i-1) and hx(i-1)^2 on the right hand side? E.g.,
x_rec(i)=c1+c2*x_rec(i-1)+c3*hx(i-1)+c4*x_rec(i-1)*hx(i-1)+c5*x_rec(i-1)^2+c6*hx(i-1)^2+c7*x_rec(i-1)^2*hx(i-1)^2;
hx(i)=d1+d2*x_rec(i-1)+d3*hx(i-1)+d4*x_rec(i-1)*hx(i-1)+d5*x_rec(i-1)^2+d6*hx(i-1)^2+d7*x_rec(i-1)^2*hx(i-1)^2;

その他の回答 (1 件)

KSSV
KSSV 2020 年 9 月 29 日
Read about element by element operations.
Example:
x = rand(1,10) ;
x^2 % throws error
x.^2 % this is what you have to use
You should use .^ instead of ^.
  3 件のコメント
KSSV
KSSV 2020 年 9 月 29 日
Again the error is clear.....you have initlaized a matrix to lower dimension and trying to save more dimensions.
Example:
A = zeros(3) ;
A(1,:) = rand(1,4) % error

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by