Identify maxima within parts of vectors
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi everyone! I have another problem with part of some code I wrote.
I can't understand where the error is because for some data series it works and for others it doesn't work, could you help me?
I'm new to matlab, sorry.
The code is very long, I'll only report the part that causes problems and the variables for 2 cases: case no. 1 works, case no. 2 doesn't work.
The goal of this part of the code is:
1) the for loop must work "r" times
2) identify the limits (lim_min(r) and lim_max(r)) of parts of the main vector (rHmm)
3) within these parts it adds the sum of the elements (hsum)
4) find sum greater than 2 and calculate the number of times where it is greater than 2
5) within the part of the vector rHmm, if hsum>2 adds in progression every one or two or three elements (based on da_sommare)
6) after the sum, it must identify the maximum within the vector and create a vector of the maximums.
That is, for the previous points 5) and 6) I'll give you an example:
da_sommare=1
part_of_vector=[2 3 4 5 10 20]
vettore_somma_progressiva=[5 7 9 15 40]
max_vettore_somma_progressiva=[40]
%if there are more parts of rHmm to consider I will have more maxima
%if from_sum=3 I will have vector_sum_progressive=[9 12 19 35].
Here's the part of code that doesn't work and I need help with.
clear all
close all
clc
load case1
dim=size(not1,2);
for r=1:dim
lim_min(r)=yDry(iNoOffDry(:,r))+not1(:,r)-not1(:,r)+dati_asciutti; %identifies the limits of the intervals
lim_max(r)=yDry(iNoOffDry(:,r))+not1(:,r)-1;
h_sum(r)=sum(rHmm([lim_min(r):1:lim_max(r)])); %sum the h within the limits
trova_eventi_sumhmaggiore2=h_sum(find(h_sum>2)); %found in the limits where the sum of h is >2mm
numero_eventi_sumhmaggiore2=length(trova_eventi_sumhmaggiore2); %calculates the number where the sum of h is >2mm
if h_sum(r)>2%
for f=lim_min(r):(lim_max(r)-d_dasommare+1);
vettore_somma_progressiva(f)=sum(rHmm(:,[f:f+(d_dasommare-1)])); %Performs the progressive sum of every 2o3 elements of the vector h
end
if r==1;
massimi_somma_progressiva=max(vettore_somma_progressiva([lim_min(r):1:(lim_max(r)-d_dasommare+1)])) ; %Find the maximum of elements within certain limits of the new vector
vettore_massimi_somma_progressiva=[massimi_somma_progressiva]; %creates the vector of maxima
else
massimi_somma_progressiva=max(vettore_somma_progressiva([lim_min(r):1:(lim_max(r)-d_dasommare+1)])) ;
vettore_massimi_somma_progressiva=[vettore_massimi_somma_progressiva massimi_somma_progressiva];
end
end
end
Thanks in advance.
0 Commenti
Risposta accettata
Voss
il 9 Set 2023
The problem is that vettore_massimi_somma_progressiva is only initialized when h_sum(r)>2 and r==1. So if the first time h_sum(r)>2 happens is when r>1, then vettore_massimi_somma_progressiva will be undefined when you try to use it (in the else block).
One solution is to initialize vettore_massimi_somma_progressiva to an empty array before the loop; then each time h_sum(r)>2 happens, just append the new massimi_somma_progressiva to it (as you are already doing in the else block). This has the advantage of simplifying the code becuase now r==1 is not a special case.
Here is the suggested new code, running with case2:
clear all
close all
clc
load case2
dim=size(not1,2);
% initialize the vector of maxima:
vettore_massimi_somma_progressiva = [];
for r=1:dim
lim_min(r)=yDry(iNoOffDry(:,r))+not1(:,r)-not1(:,r)+dati_asciutti; %identifies the limits of the intervals
lim_max(r)=yDry(iNoOffDry(:,r))+not1(:,r)-1;
h_sum(r)=sum(rHmm([lim_min(r):1:lim_max(r)])); %sum the h within the limits
trova_eventi_sumhmaggiore2=h_sum(find(h_sum>2)); %found in the limits where the sum of h is >2mm
numero_eventi_sumhmaggiore2=length(trova_eventi_sumhmaggiore2); %calculates the number where the sum of h is >2mm
if h_sum(r)>2%
for f=lim_min(r):(lim_max(r)-d_dasommare+1);
vettore_somma_progressiva(f)=sum(rHmm(:,[f:f+(d_dasommare-1)])); %Performs the progressive sum of every 2o3 elements of the vector h
end
massimi_somma_progressiva=max(vettore_somma_progressiva([lim_min(r):1:(lim_max(r)-d_dasommare+1)])) ; %Find the maximum of elements within certain limits of the new vector
vettore_massimi_somma_progressiva = [vettore_massimi_somma_progressiva massimi_somma_progressiva];
end
end
% check the result
vettore_massimi_somma_progressiva
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!