ruby on rails - Mapping the index of values in an array that match a specific value? -


disclaimer, i'm beginner.

i have array 16 digits, limited 0's , 1's. i'm trying create new array contains index values 1's in original array.

i have:

one_pos = []     image_flat.each |x|          if x == 1              p = image_flat.index(x)             one_pos << p             image_flat.at(p).replace(0)         end     end 

the image_flat array [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

with code above, one_pos returns [3, 3] rather [3, 5] i'd expect.

where going wrong?

try using each_with_index (http://apidock.com/ruby/enumerable/each_with_index) on array.

image_flat = [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  one_pos = [] image_flat.each_with_index |value, index|    if value == 1      one_pos << index   end end 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -