how to count a sequence of data arrays with cut off?

7 次查看(过去 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)
  5 个评论
rahmat
rahmat 2024-5-16
Dear VBBV
If the cutoff value changes, why does the N value remain 4?
rahmat
rahmat 2024-5-16
Dear VBBV
%example array
data_array = [2.33, 2.00, 1.60, 1.59, 1.99];
% Cutoff value
cutoff_value = 2.00;
% Truncates the array to get data above the cut off value
data_above_cutoff = data_array(data_array > cutoff_value);
% Initialize the number of sequences
jumlah_sequence = 0;
% Checking the sequence above the cut off value
for i = 1:length(data_above_cutoff)
if i > 1 && data_above_cutoff(i) ~= data_above_cutoff(i-1) + 1
break; % If there is no sequence, exit the loop
end
jumlah_sequence = jumlah_sequence + 1;
end
disp(['Number of sequences above the cut off value: ', num2str(jumlah_sequence)]);

请先登录,再进行评论。

采纳的回答

Zinea
Zinea 2024-5-17
Hi rahmat,
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 个评论
rahmat
rahmat 2024-5-18
Dear Zinea
Thank you very much. Your answer really helped me. If I change the cut off, it will still follow the existing trend.

请先登录,再进行评论。

更多回答(1 个)

Mathieu NOE
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)
N = 4
% example 2
cnr_all=[2.33 2.00 1.60 1.51 1.53];
cut_off=1.50;
[~,N] = min(cnr_all - cut_off)
N = 4
  5 个评论
rahmat
rahmat 2024-5-16
because each cut off is an input value. And the cut off value can change as an independent variable. The data generated in the array can change. However, it has a trend from largest to smallest (cut off). After the smallest value (cut off) there is a larger value and it is ignored.
Mathieu NOE
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 CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by