PDA

View Full Version : Row major order



REDEBOLT
26-08-2011, 07:51
Hi All,

I am tring to load a matrix in row major order, but I get an error on
Array Assign "Invalid delimiter."

c.f. post. (http://www.thinbasic.com/community/showthread.php?10608-Row-order-in-matrix-filling&highlight=column+major)

Must I load a matrix and then transpose it?

Regards,
Bob

Petr Schreiber
26-08-2011, 08:49
Hi!,

to assign matrix in row order, you just need to use square brackets on assignment. I just tested it in ThinBASIC 1.8.8.0:


Uses "Console"

Dim myMatrix(3, 3) As Ext

myMatrix = [ 1, 2, 3,
4, 5, 6,
7, 8, 9 ]

PrintL myMatrix(1, 1), myMatrix(1, 2), myMatrix(1, 3)
PrintL myMatrix(2, 1), myMatrix(2, 2), myMatrix(2, 3)
PrintL myMatrix(3, 1), myMatrix(3, 2), myMatrix(3, 3)

WaitKey


The approach with () mentioned in the referenced post has been replaced with [] last year, this change is also documented in Help file in What's new log.


Petr

REDEBOLT
26-08-2011, 21:45
Ok, I use square brackets, but get same error:


Dim arat(14,14) As String
Array Assign arat() =
[1, 1 , "1/2" , "1/3" , "1/4" , "1/5" , "1/6" , "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", _
1, "1/2" , "1/3" , "1/4" , "1/5" , "1/6" , "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", _
1, "1/3" , "1/4" , "1/5" , "1/6" , "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", _
1, "1/4" , "1/5" , "1/6" , "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", _
1, "1/5" , "1/6" , "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", _
1, "1/6" , "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", _
1, "1/7" , "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", _
1, "1/8" , "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", _
1, "1/9" , "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", _
1, "1/10", "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", _
1, "1/11", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", "1/23", _
1, "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", "1/23", "1/24", _
1, "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", "1/23", "1/24", "1/25", _
1, "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", "1/23", "1/24", "1/25", "1/26" ]


Bob

ErosOlmi
26-08-2011, 23:49
Dear Bob,

I'm sorry but row major order is actually supported only fro numeric matrix.
I will work in order to add it also for all kind of arrays.

Eros

PS: added request in support area
http://www.thinbasic.com/community/project.php?issueid=308

(http://www.thinbasic.com/community/project.php?issueid=308)

REDEBOLT
27-08-2011, 00:47
Thanks, Eros.
:)

danbaron
27-08-2011, 07:55
' code -----------------------------------------------------------------------
Uses "console"
'-----------------------------------------------------------------------------

Function setmatrix(ByRef b() As String, n As Integer)
Local s1, s2 As String
Local i, j As Integer

For i = 1 To %n
For j = 1 To %n
s1 = Format$(i, "00")
s2 = Format$(j, "00")
b(i,j) = s1 & s2
Next
Next
End Function

'-----------------------------------------------------------------------------

Function printmatrix(ByRef b() As String, n As Integer)
Local i, j As Integer

For i = 1 To %n
For j = 1 To %n
Print b(i,j), " "
Next
PrintL
Next
PrintL
End Function

'-----------------------------------------------------------------------------

Function transmatrix(ByRef b() As String, n As Integer)
' Transposes a square matrix in-place.

Integer i, j
Local temp As String

For i = 1 To n
For j = i+1 To n
temp = b(i,j)
b(i,j) = b(j,i)
b(j,i) = temp
Next
Next
End Function

'-----------------------------------------------------------------------------

Function TBMain()
%n = 10
Local a(%n,%n) As String

setmatrix(a,%n)
printmatrix(a,%n)
transmatrix(a,%n)
printmatrix(a,%n)

WaitKey
End Function

' output ---------------------------------------------------------------------

0101 0102 0103 0104 0105 0106 0107 0108 0109 0110
0201 0202 0203 0204 0205 0206 0207 0208 0209 0210
0301 0302 0303 0304 0305 0306 0307 0308 0309 0310
0401 0402 0403 0404 0405 0406 0407 0408 0409 0410
0501 0502 0503 0504 0505 0506 0507 0508 0509 0510
0601 0602 0603 0604 0605 0606 0607 0608 0609 0610
0701 0702 0703 0704 0705 0706 0707 0708 0709 0710
0801 0802 0803 0804 0805 0806 0807 0808 0809 0810
0901 0902 0903 0904 0905 0906 0907 0908 0909 0910
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

0101 0201 0301 0401 0501 0601 0701 0801 0901 1001
0102 0202 0302 0402 0502 0602 0702 0802 0902 1002
0103 0203 0303 0403 0503 0603 0703 0803 0903 1003
0104 0204 0304 0404 0504 0604 0704 0804 0904 1004
0105 0205 0305 0405 0505 0605 0705 0805 0905 1005
0106 0206 0306 0406 0506 0606 0706 0806 0906 1006
0107 0207 0307 0407 0507 0607 0707 0807 0907 1007
0108 0208 0308 0408 0508 0608 0708 0808 0908 1008
0109 0209 0309 0409 0509 0609 0709 0809 0909 1009
0110 0210 0310 0410 0510 0610 0710 0810 0910 1010

danbaron
28-08-2011, 06:12
' code -----------------------------------------------------------------------
Uses "console"
'-----------------------------------------------------------------------------

Function setmatrix(ByRef b() As String, n As Integer)
Local s As String
Local i,j As Integer

For i = 1 To n
b(i,1) = "1/01"
For j = 2 To n
s = Format$(i+j-2, "00")
b(i,j) = "1/" & s
Next
Next
End Function

'-----------------------------------------------------------------------------

Function printmatrix(ByRef b() As String, n As Integer)
Local i, j As Integer

For i = 1 To %n
For j = 1 To %n
Print b(i,j), ""
Next
PrintL
Next
PrintL
End Function

'-----------------------------------------------------------------------------

Function TBMain()
%n = 13
Local a(%n,%n) As String

setmatrix(a,%n)
printmatrix(a,%n)

WaitKey
End Function

' output ---------------------------------------------------------------------

1/01 1/01 1/02 1/03 1/04 1/05 1/06 1/07 1/08 1/09 1/10 1/11 1/12
1/01 1/02 1/03 1/04 1/05 1/06 1/07 1/08 1/09 1/10 1/11 1/12 1/13
1/01 1/03 1/04 1/05 1/06 1/07 1/08 1/09 1/10 1/11 1/12 1/13 1/14
1/01 1/04 1/05 1/06 1/07 1/08 1/09 1/10 1/11 1/12 1/13 1/14 1/15
1/01 1/05 1/06 1/07 1/08 1/09 1/10 1/11 1/12 1/13 1/14 1/15 1/16
1/01 1/06 1/07 1/08 1/09 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17
1/01 1/07 1/08 1/09 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18
1/01 1/08 1/09 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19
1/01 1/09 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20
1/01 1/10 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21
1/01 1/11 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21 1/22
1/01 1/12 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21 1/22 1/23
1/01 1/13 1/14 1/15 1/16 1/17 1/18 1/19 1/20 1/21 1/22 1/23 1/24