Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Math Help - For Loop for an m-by-1 matrix

  1. #16
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    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
    Follow Math Help Forum on Facebook and Google+

  2. #17
    Newbie
    Joined
    May 2010
    Posts
    10
    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 ?
    Please help me
    Thank you
    Follow Math Help Forum on Facebook and Google+

  3. #18
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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:
    Last edited by elbarto; July 9th 2010 at 10:07 PM. Reason: gave incorrect advice.
    Follow Math Help Forum on Facebook and Google+

  4. #19
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    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
    
    bad_index_list = getNextDoor(bad_flag);%see my custom function
    %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
        
        bad_index = bad_index_list(i,1);%bad index list
        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')
    Follow Math Help Forum on Facebook and Google+

  5. #20
    Newbie
    Joined
    May 2010
    Posts
    10
    Please check your PM
    Follow Math Help Forum on Facebook and Google+

  6. #21
    Newbie
    Joined
    May 2010
    Posts
    10
    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


    Quote Originally Posted by elbarto View Post
    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
    
    bad_index_list = getNextDoor(bad_flag);%see my custom function
    %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
        
        bad_index = bad_index_list(i,1);%bad index list
        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')
    Follow Math Help Forum on Facebook and Google+

Page 2 of 2 FirstFirst 12

Similar Math Help Forum Discussions

  1. simple for loop
    Posted in the Math Software Forum
    Replies: 0
    Last Post: September 29th 2011, 09:08 AM
  2. closed loop poles and feedback coefficient matrix
    Posted in the Advanced Math Topics Forum
    Replies: 0
    Last Post: May 29th 2011, 06:47 PM
  3. Matlab while loop, Please help!
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 23rd 2010, 01:14 AM
  4. Using a for loop and while loop.
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 19th 2010, 01:18 PM
  5. Loop invariants
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: July 13th 2009, 04:58 PM

Search Tags


/mathhelpforum @mathhelpforum