how to count a sequence of data arrays with cut off?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
i have 5 data in one array. For example, the data array [2.33 2.00 1.60 1.59 1.99]. If the cutoff is 1.50, it means the amount of data is 4 (pay attention to the order of the data). the number 1.99 in the 5th index is not counted.
This is my code
cnr_all=[2.33 2.00 1.60 1.59 1.99];
cut_off=1.50;
N=zeros(size(cnr_all));
for i=1:numel(cnr_all)
if cnr_all[i] >= cut_off;
N=N+1;
break;
end
end
disp(N)
5 commentaires
Réponse acceptée
Zinea
le 17 Mai 2024
I understand from the description that you want the code to stop counting when it encounters a value that is larger than its previous value, while still respecting the cutoff. Here is the code for the same:
cnr_all = [2.33 2.00 1.60 1.59 1.99];
cut_off = 1.50;
N = 0;
% Start from the second element to compare each element with its predecessor
for i = 2:numel(cnr_all)
% Check if the current element is above the cutoff and less than or equal to the previous one
if cnr_all(i) < cut_off || cnr_all(i) > cnr_all(i-1)
break; % Exit the loop if the current element is below the cutoff or larger than the previous element
end
N = N + 1;
end
% If the first element meets the initial condition, count it separately
if cnr_all(1) >= cut_off
N = N + 1;
end
disp(N)
The result from the above code is '4' as was mentioned in the question.
Hope it helps!
Plus de réponses (1)
Mathieu NOE
le 16 Mai 2024
hello
why not simply this ?
% example 1
cnr_all=[2.33 2.00 1.60 1.59 1.99];
cut_off=1.50;
[~,N] = min(cnr_all - cut_off)
% example 2
cnr_all=[2.33 2.00 1.60 1.51 1.53];
cut_off=1.50;
[~,N] = min(cnr_all - cut_off)
5 commentaires
Mathieu NOE
le 16 Mai 2024
ok - still I'd like to have from you what then is expected in that case :
%example array
data_array = [2.33, 2.00, 1.60, 1.59, 1.99];
% Cutoff value
cutoff_value = 2.00;
Voir également
Catégories
En savoir plus sur Variables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!