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 -typo in pre


From: Andrew Janke
Subject: Re: help - two different syntaxes produce different results -typo in previous
Date: Wed, 30 Oct 2019 08:55:56 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

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 via email to

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