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 '.^'.

조회 수: 38 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by