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

조회 수: 4 (최근 30일)
rahmat
rahmat 2024년 5월 16일
댓글: rahmat 2024년 5월 18일
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에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by