Azzera filtri
Azzera filtri

loops

3 visualizzazioni (ultimi 30 giorni)
ricco
ricco il 5 Nov 2011
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

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by