Extracting and inserting a "column" from/into an array

C

Chauk-Mean Proum

Hi,

For image processing, I have to do the following operations :

1/ I have an array and I want to extract every 4 element into another
array :

# r=red, g=green, b=blue, a=alpha
img_data = [ "r1", "g1", "b1", "a1", "r2, "g2", "b2", "a2" ]

=> [ "a1", "a2" ] # the array of every four elements i.e. alpha data

It's like extracting the fourth column of a matrix of n rows and 4
columns.

2/ Conversely, I have an array and I need to add elements at every 3
elements

img_data = [ "r1", "g1", "b1", "r2, "g2", "b2" ]
alpha_data = [ "a1", "a2" ] # the elements to add

=> [ "r1", "g1", "b1", "a1", "r2, "g2", "b2", "a2" ]

It's like inserting a column to a matrix of n rows and 3 columns.

I'm wondering if there is an easy or seamless way to do these
operations.
Thanks in advance for any tips.

Cheers.

Chauk-Mean.
 
M

Matt Neuburg

Array is an Enumerable so when you require 'enumerator' you get a bunch
of cool ways of slicing it up.
1/ I have an array and I want to extract every 4 element into another
array :

# r=red, g=green, b=blue, a=alpha
img_data = [ "r1", "g1", "b1", "a1", "r2, "g2", "b2", "a2" ]

=> [ "a1", "a2" ] # the array of every four elements i.e. alpha data

result = img_data.enum_slice(4).map {|arr| arr[3]}
It's like extracting the fourth column of a matrix of n rows and 4
columns.

2/ Conversely, I have an array and I need to add elements at every 3
elements

img_data = [ "r1", "g1", "b1", "r2, "g2", "b2" ]
alpha_data = [ "a1", "a2" ] # the elements to add

=> [ "r1", "g1", "b1", "a1", "r2, "g2", "b2", "a2" ]

result = img_data.enum_slice(3).zip(alpha_data).flatten

m.
 
C

Chauk-Mean Proum

Hi Matt,
result = img_data.enum_slice(4).map {|arr| arr[3]}

result = img_data.enum_slice(3).zip(alpha_data).flatten

This is exactly what I was looking for. I just replace enum_slice with
each_slice as I'm using ruby 1.9.
Thanks a lot.

Cheers.

Chauk-Mean.
 
P

Phrogz

Hi,

For image processing, I have to do the following operations :

1/ I have an array and I want to extract every 4 element into another
array :

# r=red, g=green, b=blue, a=alpha
img_data = [ "r1", "g1", "b1", "a1", "r2, "g2", "b2", "a2" ]

=> [ "a1", "a2" ]  # the array of every four elements i.e. alpha data

It's like extracting the fourth column of a matrix of n rows and 4
columns.

2/ Conversely, I have an array and I need to add elements at every 3
elements

img_data = [ "r1", "g1", "b1", "r2, "g2", "b2" ]
alpha_data = [ "a1", "a2" ] # the elements to add

=> [ "r1", "g1", "b1", "a1", "r2, "g2", "b2", "a2" ]

It's like inserting a column to a matrix of n rows and 3 columns.

I'm wondering if there is an easy or seamless way to do these
operations.

Possibly not as efficient, but if you were representing the data as a
'2D' array (array of arrays) alternatively you could .transpose the
array, insert a row, and .transpose it back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top