Jump to content
Sign in to follow this  
Kyan

Add columns to array (inverse of _ArrayUnique)

Recommended Posts

Kyan

Hi, I'm trying to add columns from existing arrays to spare time and perhaps memory when reading it

I have 3 arrays (with the same size) and want to put them togheter by columns (the inverse of _ArrayUnique function) is it possible? (and fast?)

Here's a example of _ArrayUnique():

#include <Array.au3>
Dim $aArray[6][2] = [[1, "A"],[2, "B"],[3, "C"],[1, "A"],[2, "B"],[3, "C"]]
_ArrayDisplay($aArray, "$aArray")
$aNewArray = _ArrayUnique($aArray) ;Using Default Parameters
_ArrayDisplay($aNewArray, "$aNewArray represents the 1st Dimension of $aArray")

$aNewArray = _ArrayUnique($aArray, 2) ;Using 2nd Dimension
_ArrayDisplay($aNewArray, "$aNewArray represents the 2nd Dimension of $aArray")

thanks in advance ;)


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites
Affe

I think you're confused as to what "_ArrayUnique()" does.

If I'm guessing correctly, you want to take three arrays and concatenate them. You could do this by declaring a fourth, new array, with the number of columns needed, and insert each element in like this:

Dim $array_1[5][2] = [[1, "A"], [2, "B"], [3, "C"], [4, "D"], [5, "E"]]
Dim $array_2[5][2] = [[1, "A"], [2, "B"], [3, "C"], [4, "D"], [5, "E"]]
Dim $array_3[5][2] = [[1, "A"], [2, "B"], [3, "C"], [4, "D"], [5, "E"]]

Dim $new_array[5][6]    ;5 rows and 6 columns to hold both columns from each array above

For $x = 0 To 4
    $new_array[$x][0] = $array_1[$x][0]
    $new_array[$x][1] = $array_1[$x][1]
    $new_array[$x][2] = $array_2[$x][0]
    $new_array[$x][3] = $array_2[$x][1]
    $new_array[$x][4] = $array_3[$x][0]
    $new_array[$x][5] = $array_4[$x][1]
Next

[center][/center]

Share this post


Link to post
Share on other sites
Kyan

I think you're confused as to what "_ArrayUnique()" does.

If I'm guessing correctly, you want to take three arrays and concatenate them. You could do this by declaring a fourth, new array, with the number of columns needed, and insert each element in like this:

Dim $array_1[5][2] = [[1, "A"], [2, "B"], [3, "C"], [4, "D"], [5, "E"]]
Dim $array_2[5][2] = [[1, "A"], [2, "B"], [3, "C"], [4, "D"], [5, "E"]]
Dim $array_3[5][2] = [[1, "A"], [2, "B"], [3, "C"], [4, "D"], [5, "E"]]

Dim $new_array[5][6] ;5 rows and 6 columns to hold both columns from each array above

For $x = 0 To 4
$new_array[$x][0] = $array_1[$x][0]
$new_array[$x][1] = $array_1[$x][1]
$new_array[$x][2] = $array_2[$x][0]
$new_array[$x][3] = $array_2[$x][1]
$new_array[$x][4] = $array_3[$x][0]
$new_array[$x][5] = $array_4[$x][1]
Next

Hi, thanks for help me out :)

Thats what I want to do, there ins't a faster way of doing it?

I'm leading with 4 arrays (1D) with less than 100 rows, and what I want is put them in one 2D array [n][4] (as in your example)

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites
kylomas

DiOgO,

Thats what I want to do, there ins't a faster way of doing it?

Time it and I think you'll fiind that it runs in less than .001 secs.

Here's a function that accepts up to 10 1D arrays of any size and produces a 2D array...

#include <array.au3>

local $a[20] = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $b[10] = [1,2,3,4,5,6,7,8,9,0]
local $c[50] = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $d[5]  = [1,2,3,4,5]
local $e[7] = [1,2,3,4,5,6,7]
local $f[15] = ['abc',9.333,'                      ','a1b5d7']

local $st=timerinit()

$a10 = _array_combine($a,$b,$c,$d,$e,$f)
ConsoleWrite(@error & @LF)

ConsoleWrite(round(TimerDiff($st)/1000,4) & ' seconds' & @LF)

_arraydisplay($a10)

func _array_combine($arr1,$arr2,$arr3=0,$arr4=0,$arr5=0,$arr6=0,$arr7=0,$arr8=0,$arr9=0,$arr10=0)

    ; at least two arrays required

    if not IsArray($arr1) or not IsArray($arr2) then return seterror(1,0,0)

    ; determine how many arrays passed to function and make sure that they are 1D

    for $1 = 1 to 10
        if not isarray(eval("arr" & $1)) then ExitLoop
        if ubound(eval("arr" & $1),0) > 1 then return seterror(2,0,0)
    Next

    local $num_cols = $1 - 1
    local $max_elements = 0

    ; detrmine max # of rows

    for $1 = 1 to $num_cols
        if ubound(eval("arr" & $1)) > $max_elements then $max_elements = ubound(eval("arr" & $1))
    next

    ; define and ppopulate target array

    local $aTarget[$max_elements][$num_cols+1], $aTemp

    for $1 = 0 to $num_cols
        $aTemp = eval("arr" & $1+1)
        for $2 = 0 to ubound($aTemp) - 1
            $aTarget[$2][$1] = $aTemp[$2]
        Next
    next

    return $aTarget

endfunc

kylomas

edit: runs in .0005 secs

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

Affe,

The code you posted does not run. I changed it as follows:

Local $st = TimerInit()

Local $array_1[5][2] = [[1, "A"],[2, "B"],[3, "C"],[4, "D"],[5, "E"]]
Local $array_2[5][2] = [[1, "A"],[2, "B"],[3, "C"],[4, "D"],[5, "E"]]
Local $array_3[5][2] = [[1, "A"],[2, "B"],[3, "C"],[4, "D"],[5, "E"]]
Local $array_4[5][2] = [[1, "A"],[2, "B"],[3, "C"],[4, "D"],[5, "E"]]
Local $array_5[5][2] = [[1, "A"],[2, "B"],[3, "C"],[4, "D"],[5, "E"]]

Local $new_array[5][6] ;5 rows and 6 columns to hold both columns from each array above

For $x = 0 To 4
    $new_array[$x][0] = $array_1[$x][0]
    $new_array[$x][1] = $array_1[$x][1]
    $new_array[$x][2] = $array_2[$x][0]
    $new_array[$x][3] = $array_2[$x][1]
    $new_array[$x][4] = $array_3[$x][0]
    $new_array[$x][5] = $array_4[$x][1]
Next

ConsoleWrite(round(TimerDiff($st)/1000,4) & ' seconds' & @LF)

kylomas

  • Like 1

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Kyan

DiOgO,

Time it and I think you'll fiind that it runs in less than .001 secs.

Here's a function that accepts up to 10 1D arrays of any size and produces a 2D array...

#include <array.au3>

local $a[20] = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $b[10] = [1,2,3,4,5,6,7,8,9,0]
local $c[50] = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
local $d[5] = [1,2,3,4,5]
local $e[7] = [1,2,3,4,5,6,7]
local $f[15] = ['abc',9.333,' ','a1b5d7']

local $st=timerinit()

$a10 = _array_combine($a,$b,$c,$d,$e,$f)
ConsoleWrite(@error & @LF)

ConsoleWrite(round(TimerDiff($st)/1000,4) & ' seconds' & @LF)

_arraydisplay($a10)

func _array_combine($arr1,$arr2,$arr3=0,$arr4=0,$arr5=0,$arr6=0,$arr7=0,$arr8=0,$arr9=0,$arr10=0)

; at least two arrays required

if not IsArray($arr1) or not IsArray($arr2) then return seterror(1,0,0)

; determine how many arrays passed to function and make sure that they are 1D

for $1 = 1 to 10
if not isarray(eval("arr" & $1)) then ExitLoop
if ubound(eval("arr" & $1),0) > 1 then return seterror(2,0,0)
Next

local $num_cols = $1 - 1
local $max_elements = 0

; detrmine max # of rows

for $1 = 1 to $num_cols
if ubound(eval("arr" & $1)) > $max_elements then $max_elements = ubound(eval("arr" & $1))
next

; define and ppopulate target array

local $aTarget[$max_elements][$num_cols+1], $aTemp

for $1 = 0 to $num_cols
$aTemp = eval("arr" & $1+1)
for $2 = 0 to ubound($aTemp) - 1
$aTarget[$2][$1] = $aTemp[$2]
Next
next

return $aTarget

endfunc

kylomas

edit: runs in .0005 secs

Hi kylomas, I lost how many times you helped me :)

pretty impressive, i have one question, in your function, when a variable is not a array it continues: if not isarray(eval("arr" & $1)) then ExitLoop, shouldn't be a seterror(..) in stead of ExitLoop?


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites
kylomas

DiOgO,

i have one question, in your function, when a variable is not a array it continues: if not isarray(eval("arr" & $1)) then ExitLoop, shouldn't be a seterror(..) in stead of ExitLoop?

No, this loop counts the number of vars passed to the function that are arrays. When it encounters a non-array variable it exits the loop and begins the process of creating the combined array.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Kyan

DiOgO,

No, this loop counts the number of vars passed to the function that are arrays. When it encounters a non-array variable it exits the loop and begins the process of creating the combined array.

kylomas

so, it stop when a non arrays is encountered since it have at least 2 arrays to "merge" without aborting all the process, nice thought ;)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.