how to count a sequence of data arrays with cut off?
1 次查看(过去 30 天)
显示 更早的评论
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)
采纳的回答
Zinea
2024-5-17
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!
更多回答(1 个)
Mathieu NOE
2024-5-16
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 个评论
Mathieu NOE
2024-5-16
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;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!