-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsMATOOLS.bas
More file actions
5090 lines (4250 loc) · 179 KB
/
sMATOOLS.bas
File metadata and controls
5090 lines (4250 loc) · 179 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Attribute VB_Name = "sMATOOLS"
Option Explicit
Option Compare Text
' Option Base 1
' In general, range array base start with 1 and vba array with 0 base
' Excel/VBA reads array left to right and then top to bottom. To work with range array and vba array
' together, all array will be converted to base 1
' sMATTOOLS are a collection of matrix operation and compatible with MATLAB
' Author: Yeol C. Seong
' Date: 2001/08/01
' Contact: monkeyquant@gmail.com
' REVISED: 2013/10/04
' - Added new comments to clarify the works and removed unnecessary comments
' - optimized codes
' : 2023/12/20
' - Added Inverse using Application.MINVERSE
' - Changed names to avoid any conflicts
' - Reformatted samples in worksheets
Public Function elemOp(ByVal leftArr As Variant, ByVal rightArr As Variant, ByVal cKey As Variant) As Variant
Dim op As String
If TypeName(cKey) = "Range" Then
op = LCase(cKey.Value)
Set cKey = Nothing
Else
op = LCase(cKey)
cKey = Empty
End If
Select Case True
Case (op = "add" Or op = "plus" Or op = "+")
elemOp = elemPlus(leftArr, rightArr)
Case (op = "subtract" Or op = "minus" Or op = "-")
elemOp = elemMinus(leftArr, rightArr)
Case (op = "multiply" Or op = "product" Or op = "*")
elemOp = elemMultiply(leftArr, rightArr)
Case (op = "Divide" Or op = "/")
elemOp = elemDivide(leftArr, rightArr)
Case Else
MsgBox "no such operation defined"
elemOp = "#VALUE!"
End Select
End Function
Public Function elemDivide(ByVal leftArr As Variant, ByVal rightArr As Variant) As Variant
' elemDIV: Calculate Matrix Division element by element.
'
' For Example, A={1,2; 3,4}, and B = {6,7;9,8}, Then B ./A = {6,3.5; 3,2}.
' If either number of column or rows are matching with those of array, then such row
' or column vector will be applied to entire rows or columns
'
' %%Author: Yeol C. Seong
' %%Date: 2001/08/01
' %%Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' %%REVISED:
'
Dim NoRowA As Variant, NoColA As Variant
Dim NoRowB As Variant, NoColB As Variant
Dim MAT_A As Variant, MAT_B As Variant
Dim i As Integer, j As Integer
Dim cAns() As Variant
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(leftArr) = "Range" Then
MAT_A = leftArr.Value
Set leftArr = Nothing
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(leftArr) = 0 Then
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
Else
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(rightArr) = "Range" Then
MAT_B = rightArr.Value
Set rightArr = Nothing
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(rightArr) = 0 Then
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) - LBound(MAT_B, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) - LBound(MAT_B, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To NoRowB, 1 To NoColB)
Else
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
' leftArray and rightArray both are matrix
' lieftArray is column or row vector, and vice versa
' one is row vector and the other is column vector
' else case
If NoRowA = NoRowB Then
If NoColA = NoColB Then
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
ReDim cAns(1 To NoRowA, 1 To NoColA) As Variant
For i = 1 To NoRowA
For j = 1 To NoColA
If MAT_B(i, j) <> 0 Then
cAns(i, j) = MAT_A(i, j) / MAT_B(i, j)
Else
cAns(i, j) = "#DIV/0!"
End If
Next j
Next i
elemDivide = cAns
Else
MsgBox "The size of matrix is not matched"
elemDivide = "Erro 13"
End If
Else
MsgBox "The size of matrix is not matched"
elemDivide = "Error 13"
End If
End Function
Public Function elemMultiply(ByVal leftArr As Variant, ByVal rightArr As Variant) As Variant
'
' elemPROD: Calculate Matrix Multiplication of element by element.
'
' For Example, A={1,2; 3,4}, and B = {6,7;8,9}, Then Ans = {6,14; 24,36}.
'
'
' Author: Yeol C. Seong
' Date: 2001/08/01
' Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' REVISED:
'
Dim NoRowA As Variant, NoColA As Variant ' size of left matrix
Dim NoRowB As Variant, NoColB As Variant ' size of right matrix
Dim MAT_A As Variant, MAT_B As Variant ' all arrrays including range into vba array
Dim i As Integer, j As Integer, k As Integer ' dummy variables
Dim cAns() As Variant
Dim tAns As Double ' column and row calculation
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(leftArr) = "Range" Then
MAT_A = leftArr.Value ' When we use Array as Input.
Set leftArr = Nothing ' Free up memory
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(leftArr) = 0 Then
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
Else
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(rightArr) = "Range" Then
MAT_B = rightArr.Value
Set rightArr = Nothing
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(rightArr) = 0 Then
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) - LBound(MAT_B, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) - LBound(MAT_B, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
Else
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If NoRowA = NoRowB Then
If NoColA = NoColB Then
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
ReDim cAns(1 To NoRowA, 1 To NoColA) As Variant
For i = 1 To NoRowA
For j = 1 To NoColB
cAns(i, j) = MAT_A(i, j) * MAT_B(i, j)
Next j
Next i
elemMultiply = cAns
Else
MsgBox "Error 13: The size of matrix is not matched"
elemMultiply = "Error 13"
End If
Else
MsgBox "Error 13: The size of matrix is not matched"
elemMultiply = "Error 13"
End If
End Function
Public Function elemPlus(ByVal leftArr As Variant, ByVal rightArr As Variant) As Variant
'
' elemAdd: Calculate Matrix Multiplication of element by element.
'
' For Example, A={1,2; 3,4}, and B = {6,7;8,9}, Then Ans = {6,14; 24,36}.
'
'
' Author: Yeol C. Seong
' Date: 2001/08/01
' Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' REVISED:
'
Dim NoRowA As Variant, NoColA As Variant ' size of left matrix
Dim NoRowB As Variant, NoColB As Variant ' size of right matrix
Dim MAT_A As Variant, MAT_B As Variant ' all arrrays including range into vba array
Dim i As Integer, j As Integer, k As Integer ' dummy variables
Dim cAns() As Variant
Dim tAns As Double ' column and row calculation
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(leftArr) = "Range" Then
MAT_A = leftArr.Value ' When we use Array as Input.
Set leftArr = Nothing ' Free up memory
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(leftArr) = 0 Then
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
Else
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(rightArr) = "Range" Then
MAT_B = rightArr.Value
Set rightArr = Nothing
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(rightArr) = 0 Then
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) - LBound(MAT_B, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) - LBound(MAT_B, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
Else
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If NoRowA = NoRowB Then
If NoColA = NoColB Then
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
ReDim cAns(1 To NoRowA, 1 To NoColA) As Variant
For i = 1 To NoRowA
For j = 1 To NoColB
cAns(i, j) = MAT_A(i, j) + MAT_B(i, j)
Next j
Next i
elemPlus = cAns
Else
MsgBox "Error 13: The size of matrix is not matched"
elemPlus = "Error 13"
End If
Else
MsgBox "Error 13: The size of matrix is not matched"
elemPlus = "Error 13"
End If
End Function
Public Function elemMinus(ByVal leftArr As Variant, ByVal rightArr As Variant) As Variant
'
' elemPROD: Calculate Matrix Multiplication of element by element.
'
' For Example, A={1,2; 3,4}, and B = {6,7;8,9}, Then Ans = {6,14; 24,36}.
'
'
' Author: Yeol C. Seong
' Date: 2001/08/01
' Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' REVISED:
'
Dim NoRowA As Variant, NoColA As Variant ' size of left matrix
Dim NoRowB As Variant, NoColB As Variant ' size of right matrix
Dim MAT_A As Variant, MAT_B As Variant ' all arrrays including range into vba array
Dim i As Integer, j As Integer, k As Integer ' dummy variables
Dim cAns() As Variant
Dim tAns As Double ' column and row calculation
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(leftArr) = "Range" Then
MAT_A = leftArr.Value ' When we use Array as Input.
Set leftArr = Nothing ' Free up memory
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(leftArr) = 0 Then
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
Else
MAT_A = leftArr
leftArr = Empty
NoRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
NoColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
' to be used as a Worksheet Function and VBA Array together
' all arrays from Rangestarting with base 1, not 0
If TypeName(rightArr) = "Range" Then
MAT_B = rightArr.Value
Set rightArr = Nothing
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(rightArr) = 0 Then
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) - LBound(MAT_B, 1) + 1 ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) - LBound(MAT_B, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
Else
MAT_B = rightArr
rightArr = Empty
NoRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
NoColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If NoRowA = NoRowB Then
If NoColA = NoColB Then
ReDim Preserve MAT_A(1 To NoRowA, 1 To NoColA)
ReDim Preserve MAT_B(1 To NoRowB, 1 To NoColB)
ReDim cAns(1 To NoRowA, 1 To NoColA) As Variant
For i = 1 To NoRowA
For j = 1 To NoColB
cAns(i, j) = MAT_A(i, j) - MAT_B(i, j)
Next j
Next i
elemMinus = cAns
Else
MsgBox "Error 13: The size of matrix is not matched"
elemMinus = "Error 13"
End If
Else
MsgBox "Error 13: The size of matrix is not matched"
elemMinus = "Error 13"
End If
End Function
Function matSize(ByVal targetArr As Variant, Optional ByVal cKey As Variant = -1) As Variant
' %% matSize: results the size of array, mimic MATLAB size
' %% Outputs:
' one of array
' %% Input:
' cArr Array - vector or matrix
' cKey:
' -1 or 'all' for array of all dimmension
' 1 or 'row' for row
' 2 or 'column' for column
' ...
'
' %%Author: Yeol C. Seong
' %%Date: 2001/08/01
' %%Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' %%REVISED:
'
Dim tArr As Variant
If TypeName(targetArr) = "Range" Then
tArr = targetArr.Value
Set targetArr = Nothing
Else
tArr = targetArr
targetArr = Empty
End If
On Error Resume Next
Select Case IIf(IsNumeric(cKey), cKey, LCase(Left(cKey, 1)))
Case 1, "r"
sSize = UBound(tArr, 1) + IIf(LBound(tArr, 1) = 0, 1, 0)
Case 2, "c"
sSize = UBound(tArr, 2) + IIf(LBound(tArr, 1) = 0, 1, 0)
Case -1, "a"
Dim tI As Long
tI = 1
Dim tX() As Variant
Do
ReDim Preserve tX(1 To 1, 1 To tI)
tX(1, tI) = UBound(tArr, tI) + IIf(LBound(tArr, tI) = 0, 1, 0)
tI = tI + 1
Loop Until IsError(UBound(tArr, tI))
'' Another Do While Loop Approach
'' Dim i As Integer
''
'' GetArrDim = -99
'' If Not IsArray(inArr) Then Exit Function
''
'' On Error Resume Next
'' Err.Clear
'' Do While IsNumeric(UBound(inArr, i + 1))
'' If Err.Number = 9 Then Exit Do
'' i = i + 1
'' Loop
'' GetArrDim = i
'' On Error GoTo 0
matSize = tX
Err.Clear
End Select
'' matSize = UBound(arr, whichDimension) + IIf(LBound(arr) = 0, 1, 0)
On Error GoTo 0
End Function
Public Function matResize(ByVal targetArr As Variant, ByVal nRows As Long, Optional ByVal nCols As Long = 1) As Variant
' Resize the given Array by given row and column size in the column based order.
' Total number of elements should be the same as row x column size.
' Default: Make matrix a column vector, mimic MATLAB reshape.
' All resize logic is from top to bottom and then move to next column
'
' Inputs
'
' Author: Yeol C. Seong
' Date: 2001/08/01
' Contact: yseong@uchicago.edu
' REVISED:
'
' %% Making this function available for both Worksheet and VBA
Dim tArr As Variant
If TypeName(targetArr) = "Range" Then
tArr = targetArr.Value
Set targetArr = Nothing
Else
tArr = targetArr
targetArr = Empty
End If
' Number of rows, columns and elements in the source matrix
Dim tRows As Long, tCols As Long, tE As Long
Dim i As Long, j As Long
tRows = UBound(tArr, 1) + IIf(LBound(tArr, 1) = 0, 1, 0)
tCols = UBound(tArr, 2) + IIf(LBound(tArr, 2) = 0, 1, 0)
tE = tRows * tCols
Dim tN As Long
tN = nRows * nCols
If (tE < tN) Or Not IsArray(tArr) Then
MsgBox ("Out of Scope")
Exit Function
End If
Dim tempArray As Variant
tempArray = sArray2Vector(tArr, True)
tArr = Empty
Dim tArray() As Variant
ReDim tArray(1 To nRows, 1 To nCols)
Dim tCount As Long
For j = 1 To nCols
For i = 1 To nRows
tCount = tCount + 1
tArray(i, j) = tempArray(tCount, 1)
Next i
Next j
matResize = tArray
End Function
Function repeatSequence(ByVal cArr As Variant, ByVal nRepeat As Long, Optional ByVal cDirection As Variant = "row") As Variant
' %% Repeat and resize the given Array.
' %%
' Inputs
'
' %%Author: Yeol C. Seong
' %%Date: 2001/08/01
' %%Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' %%REVISED:
'
Dim nRow As Variant, nCol As Variant
Dim MAT_A As Variant, tDirection As Variant
Dim i As Long, j As Long, k As Long
Dim cAns As Variant
Dim tRepeat As Long
Dim tAddtional As Long
Dim nLength As Long
If TypeName(cArr) = "Range" Then
MAT_A = cArr.Value
Set cArr = Nothing
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRow = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
nCol = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(cArr) = 0 Then
MAT_A = cArr
Erase cArr
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRow = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
nCol = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To nRow, 1 To nCol)
Else
MAT_A = cArr
Erase cArr
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRow = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
nCol = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If TypeName(cDirection) = "Range" Then
tDirection = cDirection.Value
Set cDirection = Nothing
Else
tDirection = cDirection
End If
If Not IsNumeric(tDirection) Then
If LCase(Left(tDirection, 1)) = "c" Then ' To the right
tDirection = 2
Else ' To down
tDirection = 1
End If
End If
'
Select Case tDirection
Case 2 'Column
If nRepeat < nCol Then
sRepeatSequence = sResize(MAT_A, nRow, nRepeat)
Else
If (nRepeat Mod nCol) = 0 Then
tRepeat = nRepeat \ nCol - 1 ' Repeat number, "\" is integer division, the same as INT(a/b) or FLOOR(A/b)
Else
tRepeat = nRepeat \ nCol
End If
'' tLength = nCol * tRepeat
'' tLength = nCol * (1 + tRepeat)
'' tAddtional = nRepeat - tRepeat * nCol
''
'' cAns = MAT_A
'' ReDim Preserve cAns(1 To nRow, 1 To tLength)
'' ReDim cAns(1 To nRow, 1 To tLengh)
''
'' For k = 1 To tRepeat
'' For j = 1 To nCol
'' For i = 1 To nRow
'' cAns(i, j + nCol) = MAT_B(i, j)
'' Next i
'' Next j
'' Next k
cAns = MAT_A
For k = 1 To tRepeat
cAns = sAppendArray(cAns, MAT_A, tDirection)
Next k
ReDim Preserve cAns(1 To nRow, 1 To nRepeat)
repeatSequence = cAns
End If
Case Else ' Row Direction
'' cAns = Application.WorksheetFunction.Transpose(MAT_A)
''
'' ReDim Preserve cAns(1 To nColA, 1 To nRowA + nRowB)
''
'' For i = 1 To nRowB
'' For j = 1 To nColA
'' cAns(j, i + nRowA) = MAT_B(i, j)
'' Next j
'' Next i
''
'' sRepeatSequence = Application.WorksheetFunction.Transpose(cAns)
If nRepeat < nRow Then
repeatSequence = sResize(MAT_A, nRepeat, nCol)
Else
If (nRepeat Mod nRow) = 0 Then
tRepeat = nRepeat \ nRow - 1 ' Repeat number, "\" is integer division, the same as INT(a/b) or FLOOR(A/b)
Else
tRepeat = nRepeat \ nRow
End If
cAns = MAT_A
For k = 1 To tRepeat
cAns = sAppendArray(cAns, MAT_A, tDirection)
Next k
cAns = Application.WorksheetFunction.Transpose(cAns)
ReDim Preserve cAns(1 To nCol, 1 To nRepeat)
'' ReDim Preserve MAT_A(1 To nRepeat, 1 To nCol)
repeatSequence = Application.WorksheetFunction.Transpose(cAns)
End If
End Select
End Function
Public Function createArray(Optional ByVal numRows As Variant, Optional ByVal numColumns As Variant, Optional zeroBased As Boolean = True) As Variant
' createArray: creates m x n matrix as a placeholder with various either zero or 1 based
' this will be used mostly in VBA, not in Worksheet.
Dim tempArray As Variant
If IsMissing(numRows) Then
If IsMissing(numColumns) Then
createArray = 0
Exit Function
Else
If zeroBased Then
ReDim tempArray(numColumns - 1)
Else
ReDim tempArray(1 To numColumns)
End If
End If
Else
If IsMissing(numColumns) Then
If zeroBased Then
ReDim tempArray(numRows - 1, 0)
Else
ReDim tempArray(1 To numRows, 1 To 1)
End If
Else
If zeroBased Then
ReDim tempArray(numRows - 1, numColumns - 1)
Else
ReDim tempArray(1 To numRows, 1 To numColumns)
End If
End If
End If
createArray = tempArray
End Function
Function appendArray(ByVal cArr As Variant, ByVal cArrToAppend As Variant, Optional ByVal cDirection As Variant = "Row") As Variant
'' Append new same size Array to the existing array. If not the same, then adjusted to the first one
'
' %%Author: Yeol C. Seong
' %%Date: 2001/08/01
' %%Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' %%REVISED:
'
Dim nRowA As Variant, nColA As Variant
Dim nRowB As Variant, nColB As Variant
Dim MAT_A As Variant, MAT_B As Variant, tDirection As Variant
Dim i As Long, j As Long
Dim cAns() As Variant
If TypeName(cArr) = "Range" Then
MAT_A = cArr.Value
Set cArr = Nothing
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
nColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(cArr) = 0 Then
MAT_A = cArr
Erase cArr
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRowA = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
nColA = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To nRowA, 1 To nColA)
Else
MAT_A = cArr
Erase cArr
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
nColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If TypeName(cArrToAppend) = "Range" Then
MAT_B = cArrToAppend.Value
Set cArrToAppend = Nothing
If Not IsArray(MAT_B) Then MsgBox ("This is not array"): Exit Function
nRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
nColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
Else
If LBound(cArrToAppend) = 0 Then
MAT_B = cArrToAppend
Erase cArrToAppend
If Not IsArray(MAT_B) Then MsgBox ("This is not array"): Exit Function
nRowB = UBound(MAT_B, 1) - LBound(MAT_B, 1) + 1 ' Measuring The length of the rows of a Matrix
nColB = UBound(MAT_B, 2) - LBound(MAT_B, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To nRowB, 1 To nColB)
Else
MAT_B = cArrToAppend
Erase cArrToAppend
If Not IsArray(MAT_B) Then MsgBox ("This is not array"): Exit Function
nRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
nColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If TypeName(cDirection) = "Range" Then
tDirection = cDirection.Value
Set cDirection = Nothing
Else
tDirection = cDirection
End If
If Not IsNumeric(tDirection) Then
If LCase(Left(tDirection, 1)) = "c" Then ' To the right
tDirection = 2
Else ' To down
tDirection = 1
End If
End If
'
Select Case tDirection
Case 2 'Column
If nRowA <> nRowB Then
cAns = MAT_A
ReDim Preserve cAns(1 To nRowA, 1 To nColA + nColB)
For j = 1 To nColB
For i = 1 To nRowA
If i > nRowB Then
cAns(i, j + nColA) = 0
Else
cAns(i, j + nColA) = MAT_B(i, j)
End If
Next i
Next j
appendArray = cAns
Else
cAns = MAT_A
ReDim Preserve cAns(1 To nRowA, 1 To nColA + nColB)
For j = 1 To nColB
For i = 1 To nRowA
cAns(i, j + nColA) = MAT_B(i, j)
Next i
Next j
appendArray = cAns
End If
Case Else
If nColA <> nColB Then
' MsgBox ("both dimensions should be the same. Resize the array to append")
cAns = Application.WorksheetFunction.Transpose(MAT_A)
ReDim Preserve cAns(1 To nColA, 1 To nRowA + nRowB)
For i = 1 To nRowB
For j = 1 To nColA
If j > nColB Then
cAns(j, i + nRowA) = 0
Else
cAns(j, i + nRowA) = MAT_B(i, j)
End If
Next j
Next i
appendArray = Application.WorksheetFunction.Transpose(cAns)
Else
cAns = Application.WorksheetFunction.Transpose(MAT_A)
ReDim Preserve cAns(1 To nColA, 1 To nRowA + nRowB)
For i = 1 To nRowB
For j = 1 To nColA
cAns(j, i + nRowA) = MAT_B(i, j)
Next j
Next i
appendArray = Application.WorksheetFunction.Transpose(cAns)
End If
End Select
End Function
Function prependArray(ByVal cArr As Variant, ByVal cArrToPrepend As Variant, Optional ByVal cDirection As Variant = "Row") As Variant
'' prepend new same size Array to the existing array. If not the same, then adjusted to the first one
' %%Author: Yeol C. Seong
' %%Date: 2001/08/01
' %%Contact: yseong@uchicago.edu or yeol.seong@bmo.com
' %%REVISED:
'
Dim nRowA As Variant, nColA As Variant
Dim nRowB As Variant, nColB As Variant
Dim MAT_A As Variant, MAT_B As Variant, tDirection As Variant
Dim i As Long, j As Long
Dim cAns() As Variant
If TypeName(cArr) = "Range" Then
MAT_A = cArr.Value
Set cArr = Nothing
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
nColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
Else
MAT_A = cArr
cArr = Empty
If LBound(MAT_A) = 0 Then
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRowA = UBound(MAT_A, 1) - LBound(MAT_A, 1) + 1 ' Measuring The length of the rows of a Matrix
nColA = UBound(MAT_A, 2) - LBound(MAT_A, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To nRowA, 1 To nColA)
Else
If Not IsArray(MAT_A) Then MsgBox ("This is not array"): Exit Function
nRowA = UBound(MAT_A, 1) ' Measuring The length of the rows of a Matrix
nColA = UBound(MAT_A, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If TypeName(cArrToPrepend) = "Range" Then
MAT_B = cArrToPrepend.Value
Set cArrToPrepend = Nothing
If Not IsArray(MAT_B) Then MsgBox ("This is not array"): Exit Function
nRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
nColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
Else
MAT_B = cArrToPrepend
cArrToPrepend = Empty
If LBound(MAT_B) = 0 Then
If Not IsArray(MAT_B) Then MsgBox ("This is not array"): Exit Function
nRowB = UBound(MAT_B, 1) - LBound(MAT_B, 1) + 1 ' Measuring The length of the rows of a Matrix
nColB = UBound(MAT_B, 2) - LBound(MAT_B, 2) + 1 ' Measuring The length of the size of columns of a Matrix
ReDim Preserve MAT_A(1 To nRowB, 1 To nColB)
Else
If Not IsArray(MAT_B) Then MsgBox ("This is not array"): Exit Function
nRowB = UBound(MAT_B, 1) ' Measuring The length of the rows of a Matrix
nColB = UBound(MAT_B, 2) ' Measuring The length of the size of columns of a Matrix
End If
End If
If TypeName(cDirection) = "Range" Then
tDirection = cDirection.Value
Set cDirection = Nothing