# Thread: For Loop for an m-by-1 matrix

1. In terms of making any suggestions, I think you just need to practice writing code in Matlab using built in features such as array slicing to minimize the amount of looping you need to perform. Of course, for complex problems like yours you are probably going to need some looping so it is very important you understand the flow of your program and different ways you can implement this. I found the easiest way to learn any new language is to write a program to perform something that you can easily check with another program. My first program in Matlab was a frame analysis program and it was a total piece of rubbish although it did give me the right answer (just took about 50 times the amount of code that I can do it in now).

Keep at it and you will have a very handy skill at the end of the day.

Regards Elbarto

2. Hi Elbarto, sorry to bother you again, but I am stuck., completely stuck. I read a book called : Mastering Matlab 7, but I found that implementing in matlab a code that can do what I told you, is way more advanced than the book and my knoledge. I am lost. What I could do ?
Thank you

3. Marcolisi,

Your code your are trying to implement isn't exactly a good place to start for a beginner. Once you Identify and bad data points, you function *should* be able to be written with a pretty basic programming approach however stinging this all into usable code is the challenge. May I ask what the code is for - Educational, Commercial or Personal? I might be able to help out a bit more on the subject, but it would be extremely useful if I could see some real data. For example, are the data points sampled at regular time intervals as it doesn't appear so in your xls file.

Regards Elbarto

Edit:

4. Try something like this:

Assumptions
(1) Bad data points are not at the start or end of the data set
(2) Data is sampled at a constant rate

See the graph for a comparison of the before and after data. Remember to put the xls file and my getnextdoor function is the same dirrectory as the file below when you run it.

Regards Elarto

Code:
clc;clear;
fname = 'try.xls';
sheetName='Sheet1';
xls = xlsread(fname, sheetName);
Ref = xls(:,2);%get second row
BC = xls(:,3);%get thrid row

%calc abs of difference
ABS_Refj_Refi = abs(Ref(2:end)-Ref(1:end-1));
%95th percentile
p = prctile(ABS_Refj_Refi,85);

bad_flag = find(ABS_Refj_Refi > p);%position of bad data

%we get all indexes that are bad and how many consective indexes there are.

out = BC;%save new variable

for i = 1:length(bad_index_list(:,1))% for each section of bad data

num_consec = bad_index_list(i,2);%how many times they repeat

L = out(bad_index-1);%left value
R = out(bad_index+num_consec+1);%right value

interp_points = interp1([0;1],[L;R],linspace(0,1,num_consec+3));%interp points
new_points = interp_points(2:end-1)';%take relevant points

out(bad_index:bad_index+num_consec) = new_points;%put new points back into saved points
end

disp('Processed data points')
disp(out)

plot(BC,'b')
hold on
plot(out,'rx')
hold off
legend('old','new')

6. Hi Elbarto, I run your code to try it, but I am getting this error message :

??? Attempted to access out(0); index must be a positive integer or logical.

Do u have any suggestions.
It seems to me that the error is related to this part of the code:

Code:

out(bad_index:bad_index+num_consec) = new_points;%put new points back into saved points
Thank you

Originally Posted by elbarto
Try something like this:

Assumptions
(1) Bad data points are not at the start or end of the data set
(2) Data is sampled at a constant rate

See the graph for a comparison of the before and after data. Remember to put the xls file and my getnextdoor function is the same dirrectory as the file below when you run it.

Regards Elarto

Code:
clc;clear;
fname = 'try.xls';
sheetName='Sheet1';
xls = xlsread(fname, sheetName);
Ref = xls(:,2);%get second row
BC = xls(:,3);%get thrid row

%calc abs of difference
ABS_Refj_Refi = abs(Ref(2:end)-Ref(1:end-1));
%95th percentile
p = prctile(ABS_Refj_Refi,85);

bad_flag = find(ABS_Refj_Refi > p);%position of bad data

%we get all indexes that are bad and how many consective indexes there are.

out = BC;%save new variable

for i = 1:length(bad_index_list(:,1))% for each section of bad data

num_consec = bad_index_list(i,2);%how many times they repeat

L = out(bad_index-1);%left value
R = out(bad_index+num_consec+1);%right value

interp_points = interp1([0;1],[L;R],linspace(0,1,num_consec+3));%interp points
new_points = interp_points(2:end-1)';%take relevant points

out(bad_index:bad_index+num_consec) = new_points;%put new points back into saved points
end

disp('Processed data points')
disp(out)

plot(BC,'b')
hold on
plot(out,'rx')
hold off
legend('old','new')

Page 2 of 2 First 12