[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: matrix: repeat last number before zero
From: |
PhilipNienhuis |
Subject: |
Re: matrix: repeat last number before zero |
Date: |
Sat, 9 Nov 2019 04:35:15 -0600 (CST) |
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