Error using ==> mpower, Matrix must be square
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Oceanography and Hydrology 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!