Borrar filtros
Borrar filtros

Function to fill an array given conditions.

6 visualizaciones (últimos 30 días)
Andrew Czeizler
Andrew Czeizler el 13 de Mzo. de 2019
Comentada: Adam el 13 de Mzo. de 2019
Hi All,
I am trying to write a function to fill an array given conditions. basiclly the user inputs two variables a max and min and given these
values it creates two new row vectors, ph and date, which is a subset of an orignal vector.
My code is given below -
function [datenew,phnew] = subsetdata(date, ph, pressure, min, max)
%create empty array
datenew=cell(1,5000);
phnew=cell(1,5000);
%loop through each point
for i=1:size(pressure)
%check condition
if (pressure(i,:)>=min & pressure(i,:)<max)
%fill array if condition met
datenew{i}=date(i);
phnew{i}=ph(i);
end
end
end
  2 comentarios
Andrew Czeizler
Andrew Czeizler el 13 de Mzo. de 2019
Editada: Andrew Czeizler el 13 de Mzo. de 2019
date, ph and pressure are row vectors. we go through and check pressure againes max and min. Then if at that point its between the two values we take date and ph at that index and fill and new vector. Returning a new vector date and ph which meets the conditions.
Adam
Adam el 13 de Mzo. de 2019
What is your question?
Also, never use min and max as variable names. These are function names in Matlab and by creating variables with those names you hide the respective functions and make them inaccessible to use.

Iniciar sesión para comentar.

Respuesta aceptada

Adam
Adam el 13 de Mzo. de 2019
Editada: Adam el 13 de Mzo. de 2019
validIdx = pressure >= minVal & pressure < maxVal;
datenew = date( validIdx );
phnew = ph( validIdx );
should do this for you without needing the loop and using logical indexing instead. I don't know why you are putting results in a cell array though. Never use a cell array when a numeric array will do the job.
  2 comentarios
Andrew Czeizler
Andrew Czeizler el 13 de Mzo. de 2019
Editada: Andrew Czeizler el 13 de Mzo. de 2019
looking sharp!
Thank you :).
Will give it a try.
I was wondering, what should I use instead? nan()?
Best,
Andrew
Adam
Adam el 13 de Mzo. de 2019
Ah, well, if you want to keep positions with NaNs inbetween then you will need to do it slightly different:
datenew = nan( size( date ) );
phnew = nan( size( date ) );
validIdx = pressure >= minVal & pressure < maxVal;
datenew( validIdx ) = date( validIdx );
phnew( validIdx ) = ph( validIdx );

Iniciar sesión para comentar.

Más respuestas (1)

Andrew Czeizler
Andrew Czeizler el 13 de Mzo. de 2019
Hi Adam!
How would you define the numeric array. Im not sure of best practice here.
many thanks,
  3 comentarios
Andrew Czeizler
Andrew Czeizler el 13 de Mzo. de 2019
so you would use a nan numeric array and then fill it based on conditions?
many thanks,
best,
anAndrew
Adam
Adam el 13 de Mzo. de 2019
If you want to retain the positions within the array then yes. You could use -1 if all true data should be positive, but NaN is more logical.

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by