simple for loop problem
Afficher commentaires plus anciens
Hi there,
I have a problem with some unwanted low value sections in my dataset, which I manually need to adjust. At the moment I'm trying to automate the process, which hopefully will speed up everything a bit. I managed to find the positions of these low value sections using the "find" function.
uplim=max(hd);
lowlim=max(hd)-mean(hd);
adj=find(hd<lowlim);
As there are more than just one low value section, I'm trying to break these up into individual sections. So firstly I found the end position of each section:
test=zeros(length(adj),1);
for i=1:(length(adj)-1);
test(i,1)=((adj(i+1,1)-adj(i,1)));
end
endsec=find(test>1);
What I would like to do know is to write a simple loop, which splits these section up and saves them seperately, so that I can call them back and adjust individually. I think I just need a simple loop, but I'm not very good with Matlab and I always struggle with these. I started with something like that:
for i=1:length(endsec)
sec(i,1)=adj(endsec(i,1):endsec(i,1),1)
end
But of course this doesn't work as I'm calling back the same values. I think I need to create a loop in the loop, but I can't find a smart way to do it. I need to get a loop which creates an output like this:
1st loop
sec(1,1)=adj(1:endsec(1,1),1)
2nd loop
sec(2,1)=adj(endsec(1,1)+1:endsec(2,1),1)
3rd loop
sec(3,1)=adj(endsec(2,1)+1:endsec(3,1),1)
Any suggestions are highly appreciated. Also if you have a better solution than a loop for this problem!
Many thanks for your help, Astrid
Réponse acceptée
Plus de réponses (4)
Image Analyst
le 8 Avr 2012
I don't think you need all those loops. First, tell us what you would do to the low value sections to "adjust them individually." Would you set all those elements to some minimum value:
lowIndexes = hd < lowlim;
hd(lowIndexes) = lowlim;
or delete them entirely from the dataset:
hd(lowIndexes) = [];
or something else?
Astrid
le 8 Avr 2012
0 votes
Astrid
le 8 Avr 2012
0 votes
Astrid
le 8 Avr 2012
1 commentaire
Andrei Bobrov
le 10 Avr 2012
Hi Astrid!
This is the same as:
ix = find(t(t2));
ix(:,end+1) = ix + 1;
id = t2(ix);
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!