Splitting an array by performing an arithmetic function in ruby -
I have an array like Ruby in [3,4,5] and I want to create For diving or multiplying sub-orange, for example, I want to multiply each number from 2, 3 and 4 into the array,
[[6,9,12], [8,12,16], [ 10,15,20]]
Next, what is the best way to calculate the total number of units? In this example, this would be 9, while returning
array.count 3.
Thanks
You find it useful to use matrix operations, especially If it is a step in incorporating the matrix, vector, and / or scalar.
Matrix 'DF DET (AR1, AR2) (matrixcolum_quaker (AR2) * matrix.ro_ vector (AR1) is required. TO_A termination def NBR_letment (AR1, Arr2) arr1.size * arr2.size end
example
arr1 = [ 3,4,5] arr2 = [3,4,5] dot (AR1, AR2) # = & gt; [[9, 12, 15], # [12, 16, 20], # [15, 20, 25]] nbr_elements (arr1, arr2) # = & gt; 9 dot ([1,2,3], [4,5,6,7]) # = & gt; [[4, 8, 12], # [5, 10, 15], # [6, 12, 18], # [7, 14, 21]] nbr_elements ([1,2,3], [4,5] ], 6,7]) # = & gt; 12 Optional
If you do not want to use matrix operations, you can: < Previous> arr2.map {| e [E] .product (arr1) .map {| E, F. E * F}}
Here is an example:
arr1 = [1,2,3] arr2 = [4,5,6,7 ] Arr2.map {| e [E] .product (arr1) .map {| E, F. E = F}} # = & gt; [[4, 8, 12], # [5, 10, 15], # [6, 12, 18], # [7, 14, 21]]
Comments
Post a Comment