Finding Percentage Over Time

5 visualizaciones (últimos 30 días)
Derrick Vaughn
Derrick Vaughn el 3 de Ag. de 2020
Comentada: Derrick Vaughn el 4 de Ag. de 2020
Hi all, I'm trying to figure out and plot how much the percentage of negative numbers in one variable (OverUnder) changes as I vary another variable (time). In my code, I have time as being a vector from 0 to 6000 with 500 points and I have QRiver_converted, which is a 10834x1 matrix. Both are used to calculate GlobalQ_Infill, which is eventually used to calculate OverUnder by subtracting it from Modeled_Infill, which is also a 10834x1 matrix. I am not sure if this is the correct way to find how the percentage of negative numbers change with a changing time variable and I'm pretty sure it would need to involve a for loop. I'm still pretty new to Matlab and don't have too much experience with for loops so any help anyone can provide would be very much appreciated! Thanks!
Example of QRiver_converted data:
91900
38100
216400
138700
38800
59600
8400
64600
1170800
90700
Exampled of Modeled_Infill data
426640000
190560000
2431630000
271570000
228380000
511230000
99120000
635950000
17759900000
1043950000
time=linspace(0,6000,500);
GlobalQ_Infill=QRiver_converted.*time;
OverUnder=Modeled_Infill-GlobalQ_Infill; % (-) = overfilled
contains_negative=find(OverUnder<0);
overfilled=(length(contains_negative))/(length(OverUnder))*100;
  2 comentarios
dpb
dpb el 3 de Ag. de 2020
contains_negative=find(OverUnder<0);
overfilled=(length(contains_negative))/(length(OverUnder))*100;
can be written simply as
overfilled=sum(OverUnder<0)/length(OverUnder)*100;
No loops or find needed -- (OverUnder<0) returns a logical array and MATLAB uses [0,1] --> [false,true] so all have to do is add up the ones to know how many there are and scale to get the percentage.
BTW, it's ok here as you have a vector, but length() is a dangerous function in Matlab -- it is defined as max(size(x)) so for x other than vectors you can get an unexpected result depending on whether the array has more rows or columns. Better practice is to use size(x,1) for rows or size(x,2) for columns.
Derrick Vaughn
Derrick Vaughn el 4 de Ag. de 2020
thanks dpb for the comment and advice!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by