Error using ==> mpower, Matrix must be square

1 visualizzazione (ultimi 30 giorni)
Saleem Sarwar
Saleem Sarwar il 2 Ott 2015
Commentato: Saleem Sarwar il 2 Ott 2015
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,

Risposte (1)

Walter Roberson
Walter Roberson il 2 Ott 2015
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 Commento
Saleem Sarwar
Saleem Sarwar il 2 Ott 2015
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.

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by