Error using ==> mpower, Matrix must be square
4 views (last 30 days)
Show older comments
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,
0 Comments
Answers (1)
Walter Roberson
on 2 Oct 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.
See Also
Categories
Find more on Oceanography and Hydrology in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!