Indexing with an array of booleans #2735
Replies: 5 comments 17 replies
-
Thanks, that is indeed a useful way to allow you to filter array items. I think it conflicts with the current behavior of
|
Beta Was this translation helpful? Give feedback.
-
Hi Jos! Thank you for looking into this! Yes Regarding the use of In my experience (not that much) is more common for So I think Let's say that we now want to add 0.1 to the even numbers. We could do it by indexing like so if we already knew what is the index of the even numbers a = [4, 5, 6]
indexOfEven = [1, 3]
a[indexOfEven] = a[indexOfEven] + 0.1
a # returns [4.1, 5, 6.1] Or we could get that index of even numbers with a = [4, 5, 6]
indexOfEven = filter(1:size(a)[1], a[x]%2 == 0) # returns [1, 3]
a[indexOfEven] = a[indexOfEven]+0.1
a # returns [4.1, 5, 6.1] If it was possible to index with an array of booleans (of the same size) then it could be possible to write a = [4, 5, 6]
a[a%2 == 0] = a[a%2 == 0] + 0.1
a # desired return [4.1, 5, 6.1] Similar to Matlab a = [4, 5, 6]
a(mod(a,2)==0) += 0.1 % returns [4.1, 5, 6.1] I found a few examples I understand that indexing is already very powerful as each index can be an integer, a vector of integers and a range (including the keyword So if the index is a vector of the same size and it contains only booleans, then it could apply a conversion to make So we could write something like mass = [12, 5, 15, 20]
# Get all that masses that are greater than 10
mass[mass>10] # expected return [12, 15, 20]
force = [5, 10, 20, 15]
# Get all the forces where the masses are less than 15
force[mass<15] # expected return [5, 10] |
Beta Was this translation helpful? Give feedback.
-
Thanks for your inputs. The notation Interesting idea to extend the index to support functions like |
Beta Was this translation helpful? Give feedback.
-
Thank you for considering this, Adding this capability to do As a summary I found references on Octave, Numpy and Pandas to do stuff similar to I just found a nice explanation on Numpy, so here it is. Numpy Filter Array Again thanks for considering this |
Beta Was this translation helpful? Give feedback.
-
Seems as though the exact desired API is not quite settled here yet, although it appears that at least allowing |
Beta Was this translation helpful? Give feedback.
-
It would be nice to index with an array of booleans, I'm including some examples and reasoning.
From this I get
IndexError: Index out of range (0 < 1)
The desired result helps in other languages like Matlab/Octave or Python-Numpy.
Example in Matlab/Octave
Example in Python-Numpy
Desired example in MathJS
I understand this might not be an easy task, and also there is a function
filter
for similar issues, nonetheless I bring this topic for considerations as I think It could help extending even more the capabilities of this library.Beta Was this translation helpful? Give feedback.
All reactions