matrix: repeat last number before zero

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

matrix: repeat last number before zero

Octave - General mailing list
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


Reply | Threaded
Open this post in threaded view
|

Re: matrix: repeat last number before zero

nrjank
> 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
https://octave.org/doc/interpreter/Advanced-Indexing.html#Advanced-Indexing


Reply | Threaded
Open this post in threaded view
|

Re: matrix: repeat last number before zero

PhilipNienhuis
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


Reply | Threaded
Open this post in threaded view
|

Re: matrix: repeat last number before zero

Octave - General mailing list
In reply to this post by nrjank
thank you..very good nrjank



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: matrix: repeat last number before zero

Octave - General mailing list
In reply to this post by PhilipNienhuis
hi phill..

good fast solution

i give you the rest of code:

maxZero=max(sum(!b2))

for i=1:maxZero
     (b2(find (b2 == 0))  =  b2(find (b2 == 0) - 1));
  endfor






--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html