# I dont understand how to correct this error in line 113

120 views (last 30 days)
Amil Ali on 16 Apr 2023
Commented: Amil Ali on 17 Apr 2023
I get and error in line 113 saying thaht the "array indices must be positive integers or logical values" - in the code below how can i fix it
113 = SumPeaks(CurrentDay) = SumPeaks(CurrentDay) + PeaksVar(i,Column);
%% Find peaks and further calculations
%Find peaks in variables using restrictions for prominence and distance
% 0.5 is lowest accepted hight any below this value is disregarded and
% 60 (1min) ,means that any two detected peaks that are less than 60 data points apart will be considered a single peak
[pks_in,locs_in] = findpeaks(transpose(H2S_in(:,3)), "MinPeakProminence",0.5,"MinPeakDistance",60); % "MinPeakProminence" and "MinPeakDistance" parameters set a threshold for peak detection by specifying the minimum and minimum distance between peaks, respectively.
[pks_out,locs_out] = findpeaks(transpose(H2S_out(:,3)), "MinPeakProminence",0.5,"MinPeakDistance",60);% "pks_in" and "pks_out" variables store the peak values, and "locs_in" and "locs_out" store their locations in the data.
%Define new variables using only rows which contain a peak
% The resulting Peaks_in and Peaks_out matrices only contain the rows with the peak values and associated timestamps, which can be used for further analysis.
Peaks_in = H2S_in(locs_in,:);
Peaks_out = H2S_out(locs_out,:);
%% Daily Average Peaks and Removal Efficiency
CleanPeaks_in = RelevantPeaks(Peaks_in,H2S_in,100); %Find corresponding peaks to Peaks_in, Max Threshold set to 100
CleanPeaks_out = RelevantPeaks(Peaks_out,H2S_out,100);
%Calculate daily averages from CleanPeaks, Max Threshold set to 2000, where 2 is values in and 4 is values out
DAPeaks_in = DailyAverageNY(CleanPeaks_in,2,2000);
DAPeaks_out = DailyAverageNY(CleanPeaks_out,4,2000);
%Functions
function PeakTable = RelevantPeaks(PeaksVar_in,Var_out,Threshold) %PeaksVar_in is Peaks_in, Var_out is variable (e.g. H2S_OUT)
PeakTable = [];
for j = 1:length(PeaksVar_in)
if PeaksVar_in(j,3)<0 || PeaksVar_in(j,3) > Threshold
continue
else
for i = 1:length(Var_out)
if Var_out(i,2) < PeaksVar_in(j,2) || Var_out(i,3) < 0 %Looking at timestamps, out must be after in, and outlet must be =>0
continue
elseif Var_out(i,3) > Var_out(i+1,3) %If the next out-value is lower than the current one, a peak has been reached
PeakTable = [PeakTable; PeaksVar_in(j,2), PeaksVar_in(j,3), Var_out(i,2), Var_out(i,3)]; %Peak_in (time), Peak_in (value), Peak_out (time), Peak_out (value)
break
else
continue
end
end
end
end
end
% Defined for estimating the average paks observed in 2022.
function DailyAveragePeaksNY = DailyAverageNY(PeaksVar,Column,Threshold) %CleanPeaks_var, Column is 2 for in and 4 for out, Threshold is max allowed peak value
FinalDay = floor(PeaksVar(length(PeaksVar),1));
SumPeaks = zeros(FinalDay,1);
AmountPeaks = zeros(FinalDay,1);
for i = 1:length(PeaksVar)
if PeaksVar(i,Column) > Threshold
continue
else
CurrentDay = floor(PeaksVar(i,1));
SumPeaks(CurrentDay) = SumPeaks(CurrentDay) + PeaksVar(i,Column);
AmountPeaks(CurrentDay) = AmountPeaks(CurrentDay) + 1;
end
end
DailyAveragePeaksNY = SumPeaks ./ AmountPeaks;
end
DGM on 16 Apr 2023
Is there any reason to believe that PeaksVar(:,1) is always >=1?
Amil Ali on 17 Apr 2023
MATLAB starts count at 1 hence i put 1 but maybe your right - do have any syggestions

Image Analyst on 16 Apr 2023

### Categories

Find more on Descriptive Statistics in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!