simple for loop problem

10 次查看(过去 30 天)
Astrid
Astrid 2012-4-8
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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-4-8
lowlim=max(hd)-mean(hd);
t = hd<lowlim;
t2 = find([true;diff(t)~=0]);
id = t2(bsxfun(@plus,find(t(t2)),[0 1]));
id(:,2) = id(:,2)-1;
out = arrayfun(@(ii)id(ii,1):id(ii,2),(1:size(id,1))','un',0);
OR
lowlim=max(hd)-mean(hd);
t = [0, (hd<lowlim).', 0];
out = arrayfun(@(i1,i2)i1:i2,strfind(t,[0 1]),strfind(t,[1 0])-1,'un',0);

更多回答(4 个)

Image Analyst
Image Analyst 2012-4-8
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
Astrid 2012-4-8
Thanks for your prompt reply. These data are heading data from a sidescan sonar survey where we had trouble with the GPS - it sometimes just stopped recording, hence low values between 0.1 and 1. I would like to adjust these values to the range of the other data, so basically just add to the whole section (sum(value of previous+following datapoint))/2.

Astrid
Astrid 2012-4-8
Thanks a lot - this works fine, although it is done in a very fancy way, with functions I've never seen before. Always good to learn new things :)!
Many thanks for your help!

Astrid
Astrid 2012-4-8
Sorry Andrei, I have one more question regarding your code. What exactly are you doing with this line:
id = t2(bsxfun(@plus,find(t(t2)),[0 1]));
Am I right that this seperating t2 in id(:,1) = 0 starting points and id(:,2) = 1 starting points?
  1 个评论
Andrei Bobrov
Andrei Bobrov 2012-4-10
Hi Astrid!
This is the same as:
ix = find(t(t2));
ix(:,end+1) = ix + 1;
id = t2(ix);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by