# matrix: repeat last number before zero Classic List Threaded 5 messages Open this post in threaded view
|

## matrix: repeat last number before zero

 look that: 4 3 2 5 0 4 0 5 5 0 0 0 6 1 4 1 i want to repeat the last element before zero it result: 4 3 2 5 4 4 2 5 5 4 2 5 6 1 4 1 it's possibile to avoid loop? thank -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: matrix: repeat last number before zero

 > it's possibile to avoid loop? thank > Yes, but I don't quite have it figured out yet.  You need to learn about different types of matrix indexing.  in particular, logical and linear indexing. try this (you could just do the last step, but each is instructive) >> a = [4 3 2 5;0 4 0 5;5 0 0 0;6 1 4 1] a =    4   3   2   5    0   4   0   5    5   0   0   0    6   1   4   1 >> a==0 ans =   0  0  0  0   1  0  1  0   0  1  1  1   0  0  0  0 >> a(a==0) ans =    0    0    0    0    0 >> shift(a,1,1) ans =    6   1   4   1    4   3   2   5    0   4   0   5    5   0   0   0 >> shift(a,1,1)(a==0) ans =    4    4    2    0    5 >> a(a==0)=shift(a,1,1)(a==0) a =    4   3   2   5    4   4   2   5    5   4   0   5    6   1   4   1 >> a(a==0)=shift(a,1,1)(a==0) a =    4   3   2   5    4   4   2   5    5   4   2   5    6   1   4   1 SO, the last two steps do it for you.  I'm not sure if there's a one-step way of filling in the stacked zeros. see https://octave.org/doc/interpreter/Index-Expressions.htmland https://octave.org/doc/interpreter/Advanced-Indexing.html#Advanced-Indexing
Open this post in threaded view
|

## Re: matrix: repeat last number before zero

 nrjank wrote >> it's possibile to avoid loop? thank >> > > Yes, but I don't quite have it figured out yet.  You need to learn > about different types of matrix indexing.  in particular, logical and > linear indexing. > > try this (you could just do the last step, but each is instructive) > >>> a = [4 3 2 5;0 4 0 5;5 0 0 0;6 1 4 1] > a = > >    4   3   2   5 >    0   4   0   5 >    5   0   0   0 >    6   1   4   1 > >>> a==0 > ans = > >   0  0  0  0 >   1  0  1  0 >   0  1  1  1 >   0  0  0  0 > >>> a(a==0) > ans = > >    0 >    0 >    0 >    0 >    0 > >>> shift(a,1,1) > ans = > >    6   1   4   1 >    4   3   2   5 >    0   4   0   5 >    5   0   0   0 > >>> shift(a,1,1)(a==0) > ans = > >    4 >    4 >    2 >    0 >    5 > >>> a(a==0)=shift(a,1,1)(a==0) > a = > >    4   3   2   5 >    4   4   2   5 >    5   4   0   5 >    6   1   4   1 > >>> a(a==0)=shift(a,1,1)(a==0) > a = > >    4   3   2   5 >    4   4   2   5 >    5   4   2   5 >    6   1   4   1 > > > SO, the last two steps do it for you.  I'm not sure if there's a > one-step way of filling in the stacked zeros. I also played a bit with it, just for fun, but I couldn't grasp the context, I generally try to avoid someone else's homework assignments :-) Where you write "I'm not sure if there's a one-step way of filling in the stacked zeros" I think there isn't, because several of the zeros that are to be filled in are in a consecutive order. You needed two steps; I think as many steps are required as the max. nr. of consecutive zeros. (Note that the question is about a vertical direction.) The nearest I got was two steps of: a(find (a == 0))  =  a(find (a == 0) - 1) A nice puzzle for a rainy weekend :-) Philip -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html