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 Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by