logical indexing shortcut?

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

logical indexing shortcut?

Peter P.-2
Hi list,

I am looking for a way to operate on certain elements of a vector using
logical indexing. I want to multiply all elements in x that are integer
multiples of four by the number 5.

x = [3 15 9 12 -1 0 -12 9 6 1];
b = rem(x, 4)==0;
x(b) = x(b)*5

I wonder if there is a way to do it in one step, without creating index
vector b? I know it works when assigning static values as in

x(rem(x, 4)==0) = -1

Any help is very welcome!
P

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: logical indexing shortcut?

Peter P.-2
Addon to my prevous posting below:

* Peter P. <[hidden email]> [2017-09-03 18:02]:

> Hi list,
>
> I am looking for a way to operate on certain elements of a vector using
> logical indexing. I want to multiply all elements in x that are integer
> multiples of four by the number 5.
>
> x = [3 15 9 12 -1 0 -12 9 6 1];
> b = rem(x, 4)==0;
> x(b) = x(b)*5
>
> I wonder if there is a way to do it in one step, without creating index
> vector b?
and without calculating the condition twice, as in
x(rem(x, 4)==0) = x(rem(x, 4)==0) * 5

thanks,
P

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: logical indexing shortcut?

Andreas Weber-6
In reply to this post by Peter P.-2
Am 03.09.2017 um 18:02 schrieb Peter P.:

> I am looking for a way to operate on certain elements of a vector using
> logical indexing. I want to multiply all elements in x that are integer
> multiples of four by the number 5.
>
> x = [3 15 9 12 -1 0 -12 9 6 1];
> b = rem(x, 4)==0;
> x(b) = x(b)*5
>
> I wonder if there is a way to do it in one step, without creating index
> vector b?

x(!rem(x, 4))*=5


HTH, Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: logical indexing shortcut?

Carlo de Falco-2
In reply to this post by Peter P.-2

> On 3 Sep 2017, at 18:11, Peter P. <[hidden email]> wrote:
>
> Addon to my prevous posting below:
>
> * Peter P. <[hidden email]> [2017-09-03 18:02]:
>> Hi list,
>>
>> I am looking for a way to operate on certain elements of a vector using
>> logical indexing. I want to multiply all elements in x that are integer
>> multiples of four by the number 5.
>>
>> x = [3 15 9 12 -1 0 -12 9 6 1];
>> b = rem(x, 4)==0;
>> x(b) = x(b)*5
>>
>> I wonder if there is a way to do it in one step, without creating index
>> vector b?
> and without calculating the condition twice, as in
> x(rem(x, 4)==0) = x(rem(x, 4)==0) * 5

x(rem(x, 4)==0) *=  5

> thanks,
> P
c.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: logical indexing shortcut?

Peter P.-2
* Carlo de Falco <[hidden email]> [2017-09-03 18:18]:

>
> > On 3 Sep 2017, at 18:11, Peter P. <[hidden email]> wrote:
> >
> > Addon to my prevous posting below:
> >
> > * Peter P. <[hidden email]> [2017-09-03 18:02]:
> >> Hi list,
> >>
> >> I am looking for a way to operate on certain elements of a vector using
> >> logical indexing. I want to multiply all elements in x that are integer
> >> multiples of four by the number 5.
> >>
> >> x = [3 15 9 12 -1 0 -12 9 6 1];
> >> b = rem(x, 4)==0;
> >> x(b) = x(b)*5
> >>
> >> I wonder if there is a way to do it in one step, without creating index
> >> vector b?
> > and without calculating the condition twice, as in
> > x(rem(x, 4)==0) = x(rem(x, 4)==0) * 5
>
> x(rem(x, 4)==0) *=  5

Thank you Andy, Carlo! What a quick reply from you two! Very elegant!

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave