help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help - two different syntaxes produce different results


From: Nicholas Jankowski
Subject: Re: help - two different syntaxes produce different results
Date: Wed, 30 Oct 2019 09:33:49 -0400

On Wed, Oct 30, 2019 at 8:26 AM Joe Tusek <address@hidden> wrote:

Hi,

 

When I run the following code

 

v = [68   73   77   85

   95   85  104   99

   68  181  162   92

   98  130  123   93

   94   87   87   88]

 

[a, b]= find(v>100)

v(a,b)= 0

 

av = v = [68   73   77   85

   95   85  104   99

   68  181  162   92

   98  130  123   93

   94   87   87   88]

 

[c]= find(av>100)

av(c)= 0

 

The top script produces 6 cells with 0 in them and the bottom script produces 5 cells with zero in them. It seems to me that these should produce the same result?



You're using two different types of indexing.  In the first case you're calling v with two vectors, and the output in such a case is "the Cartesian product of the indices in the respective dimensions".  so an assignment of 0 to the locations included in that call will include all combinations of (a,b).  a includes 2, 3, and 4  and b includes 2, 3, so your assignment goes into the same cells you would get from v([2:4,2:3]).

in the second case, you're using linear indexing.  c is a vector of five positions of >100 valued elements
>> c = find(v>100)
c =

    8
    9
   12
   13
   14

the linear index refers to the position of elements of v as they are stored in memory. (the order is column-wise, so 68, 95, 68, 98, 94, 73, 85, ...), just like you would get from the output of v(:)

an assignment to v(c) is an assignment to just those 5 elements.

see
https://octave.org/doc/interpreter/Index-Expressions.html 
 
and 
https://octave.org/doc/interpreter/Advanced-Indexing.html#Advanced-Indexing 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]