How to continue while loop if NaN value is present in cell array
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Luke McCauley
el 15 de Jun. de 2020
Comentada: Luke McCauley
el 17 de Jun. de 2020
I am using a cell 1x54 cell array containing 600x8 double values. In this array, i am looking at one column in particular for each array cell (column 5) and i want to count the number of seconds it takes until the data crossess a threshold value.
I am able to simply do this with a while loop except my data has NaN values which are causing the while loop to stop. I want to count these NaN values and keep the loop progressing until that threshold is reached.
I have a row vector 'Timetosteady' which i want to count the steps into (eventually making it a 1x54 vector) and already have calculated a threshold "MinC" which is a 1x54 row vector containing threshold values.
I am encountering two issues. Firstly using this loop trying to calculate across all cells in the cell array:
TimetosteadyC=NaN(1,54);
i=1;
for j=1:54
while ((C{1,j}(i,5))< MinC(i) || isnan(C{1,j}(i,5)))
TimetosteadyC(j)=i+1;
i=i+1;
end
end
i receive the error "Index exceeds the number of array elements (54).". There defintely are 54 columns in my array (for example if i use C{1,54}(1,5) i receive a value) so why is this occuring?
Secondly, if i use this code only assessing in the first cell array:
TimetosteadyC=NaN(1,54);
i=1;
for j=1
while ((C{1,j}(i,5))< MinC(i) || isnan(C{1,j}(i,5)))
TimetosteadyC(j)=i+1;
i=i+1;
end
The code does skip the first NaN value seen in the column i am looking at within the array but then stops before the threshold has been reached (the 'Timetosteady' value spits out as 25 when it should be closer to 150)
I would really appreciate guidance on how to best create a loop in a cell array that sequentially counts the values in a specific column until the threshold has been crossed while including NaN values.
btw i am on R2019b
Thanks
0 comentarios
Respuesta aceptada
Matt J
el 15 de Jun. de 2020
Editada: Matt J
el 16 de Jun. de 2020
Since all your arrays C{j} are the same size, it seems unnecessary to carry the data around in a cell array at all. You could just concatenate them into a 3D array:
c=cat(3,C{:});
Now, you can generate the result using simple numeric array indexing. Also, no loops are required:
c5=c(:,5,:);
[~,Timetosteady]=min( c5<reshape(MinC,1,1,[]) | isnan(c5) );
Timetosteady=Timetosteady(:);
3 comentarios
Más respuestas (0)
Ver también
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!