loops

3 次查看(过去 30 天)
ricco
ricco 2011-11-5
Is there a better way of calculating values from within a matric without the use of a loop. For example: I need to calculate the density of freshwater, so I use a well known equations which calculates density. The change in density is due to the change in temperature. The loop that I set up runs through the matrix of temperature and calculates the density at each point.
for i=1:size(temp,1); rho(i,:)=(1-((temp(i,:)+288.9414)./(508929.2.*(temp(i,:)+68.12963))).*((temp(i,:)-3.9863).^2)).*1000; end
This seems to work fine, but as I have such a large dataset it takes a long time to run. Is there a way of doing this calculation without having to use a loop?
thanks

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-11-5
e.g.
temp = randi(40,5)
rho2=(1-((temp+288.9414)./(508929.2*(temp+68.12963))).*((temp-3.9863).^2))*1000
  1 个评论
ricco
ricco 2011-11-5
Didn't realise it was that simple, thank you very much.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by