help - two different syntaxes produce different results

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

help - two different syntaxes produce different results

Joe Tusek

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?

 

I am using W10 and Oct 5.1.0.

 

Regards
Joe

 

Joe Tusek (BE MBA)
Technical Director
23 Warabrook Boulevard, Warabrook, NSW 2304
P +61 2 4961 9000  M +61 418 669 250

 

 



Reply | Threaded
Open this post in threaded view
|

Re: help - two different syntaxes produce different results

nrjank
On Wed, Oct 30, 2019 at 8:26 AM Joe Tusek <[hidden email]> 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
 
and