Sign in to follow this  
Followers 0
d0n

switching array columns and rows

10 posts in this topic

Right now i have an array with 250 columns and 5 rows

i was wondering how would i go about switching them to 250 rows and 5 columns

Share this post


Link to post
Share on other sites



One way would be create a new array and transfer the values over to it.

Share this post


Link to post
Share on other sites

#include <Array.au3>

Global $avArray[3*4*5], $iCounter = 0

For $i = 0 To 3*4*5-1
    $avArray[$i] = Random(-200, 200)
Next

For $i = 0 To 2
    For $j = 0 To 4
        For $k = 0 To 3
            ConsoleWrite($avArray[$i*$j*$k] & @LF)
            $iCounter += 1
        Next
    Next
Next

ConsoleWrite('3 * 4 * 5 = ' & $iCounter & @LF)
$iCounter = 0

For $i = 0 To 4
    For $j = 0 To 3
        For $k = 0 To 2
            ConsoleWrite($avArray[$i*$j*$k] & @LF)
            $iCounter += 1
        Next
    Next
Next

ConsoleWrite('3 * 4 * 5 = ' & $iCounter & @LF)
Exit

If you'll look closely you'll see that you're accessing the array differently.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@Authenticity: i dont really get what you are doing there tbh lol

@bchris01: trying that now

Question, how do you add to a specific array kind of like _ArrayAdd but for 2D arrays?

Edited by d0n

Share this post


Link to post
Share on other sites

Do a search for Array2D. There are a couple UDF's written for working with 2D Arrays.

Share this post


Link to post
Share on other sites

#include <Array.au3>

Dim $avArr[2][4] = [[1, 2, 3, 4], [5, 6, 7, 8]]

; ...
$iRow = UBound($avArr, 1)
$iCol = UBound($avArr, 2)
ReDim $avArr[$iRow+1][$iCol]
$avArr[$iRow][0] = 9

_ArrayDisplay($avArr)

I think, by modifying a little bit you can add to an arbitrary size/dimensions array.

Share this post


Link to post
Share on other sites

Thanks got it to work now, anyway to improve it?

$row = UBound($avArray,1)
$col = UBound($avArray,2)

Local $avArray2[2][2] = [[""],[""]]
ReDim $avArray2[$col][$row]
For $i=0 To $col-1
    For $j=0 To $row-1
        $avArray2[$i][$j] = $avArray[$j][$i]
    Next
Next
_ArrayDisplay($avArray2)

Share this post


Link to post
Share on other sites

Thats something like I was thinking. I dont think there is going to be a much faster way..

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

For anyone looking for a cut and paste UDF:

Func _ArrayColumnsToRows (ByRef $avArray)
$row = UBound($avArray,1)
$col = UBound($avArray,2)
 
Local $avArray2[$col][$row]
For $i=0 To $col-1
For $j=0 To $row-1
$avArray2[$i][$j] = $avArray[$j][$i]
Next
Next
Return $avArray2
EndFunc ;=> _ArrayColumnsToRows
Edited by icu

Share this post


Link to post
Share on other sites

Thanks got it to work now, anyway to improve it?

Why 2 lines here

Local $avArray2[2][2] = [[""],[""]]
ReDim $avArray2[$col][$row]

One line is sufficient

Local $avArray2[$col][$row]

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  
Followers 0