Replacing 0 in a vector depending on the previous number

Thread Starter

holper

Joined Jul 12, 2014
3
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
 

Attachments

djsfantasi

Joined Apr 11, 2010
9,163
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
Rich (BB code):
input number // from vector
while not eof // or vector; assuming its in a file
	if number != 0 {
		set savednumber=number // save the non-zero number
		set counter=3
		while counter >= 0 { // write it out to the output stream
			output savednumber
			input number // overwrite any previous values by 
                                          // reading and ignoring them.
			counter -= 1
			}
		}
	elseif {
		output 0
		}
	}
 

Thread Starter

holper

Joined Jul 12, 2014
3
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
 
Top