help - two different syntaxes produce different results -typo in previous

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

help - two different syntaxes produce different results -typo in previous

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 = [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 -typo in previous

Andrew Janke-2
On 10/30/19 8:30 AM, Joe Tusek 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 = [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


Hi Joe,

This is a little bit surprising, but correct. When using the multi-argument subscripting form of assignment `v(a,b) = ...`, it doesn't select individual elements identified by the corresponding a(i) and b(i) subscripts. Rather, it selects all of the elements along the dimensions identified by the values in a and b, and "intersects" those selections.

With these values from the find() operation:

a =
     3
     4
     2
     3
     4
b =
     2
     2
     3
     3
     3

When they're used as subscripts in assignment, the repetitions of values don't matter, and neither do their order (unless you were using a nonscalar RHS), so `v(a, b) = ...` is basically equivalent to `v([2 3 4], [2 3]) = ...` which selects all the elements which are in rows 2, 3, and 4 and in columns 2 and 3.

`av(c) = 0` is the correct form in this case. If you were starting out with those a and b subscripts, you'd need to use sub2ind() to convert them to linear indexes: `v(sub2ind(a, b)) = 0`.

Cheers,
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: help - two different syntaxes produce different results -typo in previous

Carlo de Falco-2
In reply to this post by Joe Tusek


> Il giorno 30 ott 2019, alle ore 13:30, Joe Tusek <[hidden email]> ha scritto:
>
> 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 = [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?


No, this is the expected behaviour, when indexing a matrix with two lists of indices you are actually indexing a sub-block,
while when indexing it with one list of indices you are acessing it as a flattened linear array.

To get the same result in the two cases above you should modify the first one as follows:

>> 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);
>> ab = sub2ind (size (v), a, b);
>> v(a,b)= 0;


>  
> I am using W10 and Oct 5.1.0.
>  
> Regards
> Joe


c.





Reply | Threaded
Open this post in threaded view
|

Re: help - two different syntaxes produce different results -typo in previous

Pantxo
Carlo de Falco-2 wrote

> To get the same result in the two cases above you should modify the first
> one as follows:
>
>>> 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);
>>> ab = sub2ind (size (v), a, b);
>>> v(a,b)= 0;

As a side remark, you could also obtain the correct result with a third
indexing form, namely "logical indexing", which is often faster since it
avoids the "find" step:

v = [68   73   77   85
   95   85  104   99
   68  181  162   92
   98  130  123   93
   94   87   87   88];
v(v>100) = 0

Pantxo




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html