Error using ==> mpower, Matrix must be square

조회 수: 2 (최근 30일)
Saleem Sarwar
Saleem Sarwar 2015년 10월 2일
댓글: Saleem Sarwar 2015년 10월 2일
I am trying to write a script for runoff calculation. Following part of the code is not working
Runoff = ones(size(data));
>> ExcessRainfall = ones(size(rainfall));
>> if rainfall - 0.2*S < 0 ;
ExcessRainfall = 0;
else ExcessRainfall = (rainfall - 0.2*S)^2/(rainfall + 0.8*S);
end
Where rainfall and S are time series daily data of 5 years.
I am getting the following message
Error using ==> mpower
Matrix must be square.
Could you please suggest me correct code for this.
Thanks and regards,

답변 (1개)

Walter Roberson
Walter Roberson 2015년 10월 2일
ExcessRainfall = (rainfall - 0.2*S).^2 ./ (rainfall + 0.8*S);
The fact that you encountered this error tells us that rainfall is a vector or matrix. In that case your test
if rainfall - 0.2*S < 0
is going to compute a vector or matrix of logical values and then apply "if" to that vector or matrix. When you apply "if" to a logical vector or matrix, the condition is only considered true if all of the elements in the vector or matrix are true, as if you had written
if all(rainfall(:) - 0.2*S < 0)
If your rainfall matrix or vector has a mix of values where some are true and some are false then the "if" will be considered false and the body of the "if" will not be executed.
In the case where all of them did happen to be true, you would be assigning the scalar 0 to ExcessRainfall even if rainfall is not a scalar, but in the case where at least one of them is false, you are assigning ExcessRainfall to be a vector the same size or shape as rainfall .
You have fallen into the common trap of thinking of your input as a scalar when it is a vector or matrix.
  댓글 수: 1
Saleem Sarwar
Saleem Sarwar 2015년 10월 2일
Could you please suggest me proper script for this analysis? I have to compute excess rainfall fulfilling these two conditions 1. if Rainfall - 0.2*S < 0, then excess rainfall will be 0 otherwise excess rainfall = (Rainfall -0.2*s)^2/ Rainfall + 0.8*S) where rainfall and S are daily time series data of years.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by