How to compare future vector value to a past one in a while loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I am using MATLAB R2020a on a MacOS. I have while loop whereby the current input sample is compared to the previously 'accepted' sample and is then added to a vector if it meets certain conditions. This creates a vector of 'accepted' values at the end of the input vector, whilst retaining the indices of both the accepted and rejected input samples. However, for the rejected samples, instead of having a 'blank' or [ ] for that index, I would like it to be the mean of the framing 2 accepted values. In other words, the rejected sample should be replaced by the mean of the future next accepted input sample and the previously accepted value. However, I am not sure how to implement this into my code without it overriding previous values. Any help about fixing the current method/ alternative approaches would be much appreciated. Thanks in advance
accepted_means = zeros(length(cycle_periods_filtered),1); % array for accepted exponentially weighted means
accepted_means(1) = cycle_periods_filtered(1);
k = zeros(length(cycle_periods_filtered),1); % array for accepted raw cycle periods
k(1) = cycle_periods_filtered(1);
tau = k/3; % pre-allocation for efficiency
i = 2; % index for counting through input signal
j = 2; % index for counting through accepted exponential mean values
while i <= length(cycle_periods_filtered)
mavCurrent = (1 - 1/w(j))*accepted_means(j - 1) + (1/w(j))*cycle_periods_filtered(i);
if cycle_periods_filtered(i) < 1.5*(accepted_means(j - 1)) && cycle_periods_filtered(i) > 0.5*(accepted_means(j - 1)) % Identify high and low outliers
accepted_means(j) = mavCurrent;
k(j) = cycle_periods_filtered(i);
tau(j) = k(j)/3;
cycle_index3(j) = i;
if cycle_periods_filtered(i - 1) > 1.5*(accepted_means(j - 1)) % attempt to backtrack to previous point
k(j - 1) = (k(j) + k(j - 2))/2;
else
if cycle_periods_filtered(i - 1) < 0.5*(accepted_means(j - 1))
k(j - 1) = (k(j) + k(j - 2))/2;
end
end
j = j + 1;
end
i = i + 1;
end
% Scrap the tail
accepted_means(j - 1:end)=[];
tau(j - 1:end) = [];
k(j - 1:end) = [];
cycle_index3(j - 1:end) = [];
% Indices of valid and invalid elements
idxCycle = [1:length(cycle_periods_filtered)];
cycle_periods_filtered(cycle_index3); % List of valid points
cycle_periods_filtered(idxCycle(~ismember(idxCycle, cycle_index3))); % List of removed outliers
0 Kommentare
Antworten (1)
KALYAN ACHARJYA
am 19 Nov. 2020
How to compare future vector value to a past one in a while loop
In such case you may use cell array to store the invidivual array data, and compare it.
i=1;
while true
%Lets say data is the array result
% This "data" value changes in every iteration
array_result{i}=data;
i=i+1;
end
% Now all vector result are stored on array_result variable, you may access any elements by array_result{1},array_result{2}..so on, in compare case you can do the same within the same loop or other also.
loop
%Compare except first iteration
if i>1
result{i}=isequal(data{i+1},data{i-1});
% Or any other ways to compare, you may check the logical ways also
end
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Elementary Math finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!