How to continue while loop if NaN value is present in cell array
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Luke McCauley
il 15 Giu 2020
Commentato: Luke McCauley
il 17 Giu 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 Commenti
Risposta accettata
Matt J
il 15 Giu 2020
Modificato: Matt J
il 16 Giu 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!