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

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

Hi Rahmat,
Can you explain why the 5th index element(1.99) is not included ? Because the first four elements are also greater than the cut off. Can you explain clearly how does this cutoff workflow/method work ?
% e.g. array
cnr_all=[2.33 2.00 1.60 1.45 1.99];
% cut-off
cut_off=1.50;
N=0;
for i=1:numel(cnr_all)
if cnr_all(i) >= cut_off;
N=N+1;
%break; %comment the break
end
end
disp(N)
4
because I want a trend in the data. For example, there is data from the largest to the smallest (cutoff), but after the smallest data (cutoff) there is data that exceeds the cutoff value. This data is ignored.
For example, A=[5,4,3,2,3] cutoff =2, so the value 3 at index 5 is ignored.
Dear VBBV
If the cutoff value changes, why does the N value remain 4?
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

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개)

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

If the cutoff value changes, why does the N value remain 4?
Dear Mathieu NOE how about this?
%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)]);
well, I believed I understood yor goal - as you described above
because I want a trend in the data. For example, there is data from the largest to the smallest (cutoff), but after the smallest data (cutoff) there is data that exceeds the cutoff value. This data is ignored.
but what would you expected in this new case ?
%example array
data_array = [2.33, 2.00, 1.60, 1.59, 1.99];
% Cutoff value
cutoff_value = 2.00;
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.
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;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2024년 5월 16일

댓글:

2024년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by