Logical Array Indexing

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

Logical Array Indexing

Nikolai Huckle-2
Hi everyone,

I have a question regarding the use of logical arrays or vectors in indexing.

As you can see in the attached .m-file, I have a couple of conditions, that I want to apply to the calculation of a vector.
I would like to apply this logical vector to the calculated vector, so that if the logical index says "1" (true) it keeps the respective value but
if the logical index says "0" (false) it deletes the respective value.
Of course I could do this with some kind of nested for/if statement, but I think there would be a more elegant solution.

There is an example in the Octave Wiki (http://wiki.octave.org/Tips_and_tricks#Vectorizing_Tricks) which is close to what I want to do, but I don't really understand the code in the example.
Maybe an explanation of the code example would also suffice for me to solve the problem on my own.

Normally I would do this with nested if-conditions inside a for-loop (also included in the attached file), but I'm trying to vectorize my code a bit more.

Thanks in advance for any help.

Kind regards,

Nikolai


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave

snippet.m (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Logical Array Indexing

Jordi Gutiérrez Hermoso-2
On 20 December 2012 07:23, Nikolai Huckle <[hidden email]> wrote:

> As you can see in the attached .m-file, I have a couple of
> conditions, that I want to apply to the calculation of a vector. I
> would like to apply this logical vector to the calculated vector, so
> that if the logical index says "1" (true) it keeps the respective
> value but if the logical index says "0" (false) it deletes the
> respective value.

The vector you are looping over would be the same vector you index.

So you should write something like

    Ra = linspace(100,500,50);

    idx     = ...  ## etc
    Vaplot  = ... Ra(idx) ...

By the way, I notice that you're putting the dot from .^ and .* into
.+ and .-. The dot doesn't do anything in the latter two. Matrix
addition and subtraction is already entry-by-entry, but matrix
multiplication and matrix exponentials are very different from the
corresponding entry-by-entry operations.

Also note that "else (condition) endif;" is a syntax error. The else
statement doesn't take a condition like the if statement does.

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave