Replacing 0 in a vector depending on the previous number

Discussion in 'Programmer's Corner' started by holper, Jul 12, 2014.

  1. holper

    Thread Starter New Member

    Jul 12, 2014
    3
    0
    Hi all

    I have the following question:

    I have a vector (see attached input example). The vector consists of the numbers 0, 1, 2, 3, 4.
    And I would like to replace four (just an example, it can also be five, six or seven) 0 cells after each 1, 2, 3, 4 with the sam enumber that appeared before (see attached output example).
    How would you write this?

    Thank you in advance.

    Lisa
     
  2. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,812
    834
    Do you strictly mean replace? I mean, what if your input looks like 0,1,0,1,0,1? Your repeating value would overwrite other non-zero values. What would you like to do in that case?

    Here is some pseudo-code that may (I haven't debugged nor ran it) be an approach to your question. It has at least one problem in the case where a non-zero value appears within the repetition size of the last entry
    Code ( (Unknown Language)):
    1. input number // from vector
    2. while not eof // or vector; assuming its in a file
    3.     if number != 0 {
    4.         set savednumber=number // save the non-zero number
    5.         set counter=3
    6.         while counter >= 0 { // write it out to the output stream
    7.             output savednumber
    8.             input number // overwrite any previous values by
    9.                                           // reading and ignoring them.
    10.             counter -= 1
    11.             }
    12.         }
    13.     elseif {
    14.         output 0
    15.         }
    16.     }
    17.  
     
  3. holper

    Thread Starter New Member

    Jul 12, 2014
    3
    0
    Hi
    Many thanks for you message.
    The case that the replacing value will overwrite other non-zer values, doesn't matter. Therefore your code may work.

    However, I tested it in Matlab, but it may be written in a another language?

    thanks for a short reply again.
    best
    Lisa
     
  4. holper

    Thread Starter New Member

    Jul 12, 2014
    3
    0
    solved!!!
    lisa
     
  5. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,812
    834
    We'd like to know how you solved the problem. Others who may have a similar problem then could benefit from your experience.
     
Loading...