help-octave
[Top][All Lists]

## Re: matrix: repeat last number before zero

 From: Nicholas Jankowski Subject: Re: matrix: repeat last number before zero Date: Fri, 8 Nov 2019 20:53:43 -0500

```> 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.html

and