"for" loop question
7 views (last 30 days)
Show older comments
Hello,
Below is an excerpt from my code, in which I wish to linearly fix a baseline drift. The variable "QLBrainShift1" is simply the original signal.
AmtOfPts1 = iSO(3) - iSO(2); % Amount of points between SO 2 and 3
error1 = -QLBrainShift1(end); % scalar
for point1 = AmtOfPts1:-1:0
QLBrainShift1a = QLBrainShift1+error1-(point1/AmtOfPts1)*error1;
end
figure
plot(QTime(iSO(2):iSO(3)),QLBrainShift1a)
The plot that is being returned seems like QLBrainShift1a is being overwritten upon each loop. Therefore, only the last loop (point1=0) exits the "for" loop, and gets plotted. My intention is to create a new signal (QLBrainShift1a) according to the equation above with varying "point1" values, and plot all of the new signal on 1 graph. Any help is greatly appreciated.
Thanks, NH
0 Comments
Accepted Answer
Matt J
on 7 Feb 2013
Edited: Matt J
on 7 Feb 2013
Perhaps as follows? Basically, you've forgotten to index the element of QLBrainShift1a (and maybe also QLBrainShift1) that you're trying to operate on.
point1=AmtOfPts1:-1:0;
for i=1:length(point1)
QLBrainShift1a(i) = QLBrainShift1(i)+error1-(point1(i)/AmtOfPts1)*error1;
end
0 Comments
More Answers (2)
Cedric
on 7 Feb 2013
Edited: Cedric
on 7 Feb 2013
I can't figure out what you are trying to do, but here are some bits of code that may simplify the discussion..
Assume that we have the following
>> data = [4, 5 ,8, 7, 10, 9, 10, 11] ;
>> plot(data, '*') ; % Just to visualize.
which follows a line except for two points. If you wanted to subtract e.g. the 1st value from all elements of data, you would do
>> data = data - data(1)
data =
0 1 4 3 6 5 6 7
>> plot(data, '*') ; % Just to visualize.
If you wanted to e.g. subtract values according to the slope between data(1) and data(end), you could do
>> n = numel(data) ;
>> slope = (data(end)-data(1))/(n-1) ;
>> data = data - (0:n-1)*slope
data =
0 0 2 0 2 0 0 0
>> plot(data, '*') ; % Just to visualize.
All that could be contracted into
>> data = [4, 5 ,8, 7, 10, 9, 10, 11] ;
>> n = numel(data) ;
>> data = data - data(1) - (0:n-1)*(data(end)-data(1))/(n-1) ;
Is there something in there that could be adapted to your case? If not, you should maybe provide a short example of the data that you have and what you would like to obtain.
0 Comments
See Also
Categories
Find more on Audio Processing Algorithm Design 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!