所以也许它的星期一,也许我很愚蠢..但我不能为我的生活找出一个好的方法来从2D阵列中的一行中获取一维数组。 (也许它不是一个“真正的”2D阵列?)
无论如何,我有一个我定义的数组: dim myArr(2,4) as variant
我用值1 - 15填充它,所以它看起来像:
1,2,3,4,5 6,7,8,9,10 11,12,13,14,15所以现在我想要的是该数组的单个行。 我能弄明白的唯一方法就是这样做:
dim temp() as variant ReDim temp(lbound(myArr,2) to ubound(myArr,2)) For i = 0 To 0 For j = LBound(myArr, 2) To UBound(myArr, 2) temp(j) = myArr(i, j) Next Next但我不喜欢那样,因为如果阵列变得庞大,那么花费的时间可能会相当长。 我想我应该可以做到:
dim temp as variant temp = myArr(0) 'since myArr(0) is a 1D array with 5 spots right?但是没有..我得到'错误的维数'错误。
我还通过之前的事情挖掘并发现了这个问题: 如何比较工作表中的两个完整行,并且tim thomas的答案显示转置的使用,并提及您是否比较仅使用转置一次的列,但是它不起作用..如果我让它变成这样: dim myArr(2,0) as variant transpose的dim myArr(2,0) as variant ,但不是我现在拥有的方式。
我在C ++和Python等其他语言中发现了无数的这个问题的答案,但我对这两者都不熟悉,所以我无法解释它们。
有没有更好的方法来做到这一点? 或者我坚持这种我不喜欢的双重循环?
谢谢!
So maybe its monday, maybe I'm stupid.. But I can't for the life of me figure out a good way to get a 1D array from a one row in a 2D array. (maybe it's not a "real" 2D array?)
Anyways, I have an array that I defined like this: dim myArr(2,4) as variant
I filled it with values 1 - 15 so it looks like:
1,2,3,4,5 6,7,8,9,10 11,12,13,14,15So now what I want is a single row of that array. the only way I can figure out is by doing this:
dim temp() as variant ReDim temp(lbound(myArr,2) to ubound(myArr,2)) For i = 0 To 0 For j = LBound(myArr, 2) To UBound(myArr, 2) temp(j) = myArr(i, j) Next NextBut I don't like that because if the array got huge, then the time it took to do that might be considerably longer. I THOUGHT I should be able to do:
dim temp as variant temp = myArr(0) 'since myArr(0) is a 1D array with 5 spots right?but no.. I get a 'wrong number of dimentions' error.
I also dug back through previous things and found this question: How to compare two entire rows in a sheet and the answer by tim thomas shows the use of transpose, and mentions if you were comparing columns you'd use only transpose once, but it doesn't work.. if I made it like: dim myArr(2,0) as variant the transpose works, but not the way I have it now.
I found countless answers to this question in other languages like C++ and Python but I'm not familiar at all with either so I couldn't interpret them.
Is there a better way of doing this? Or am I stuck with this diouble loop that I don't like?
Thanks!
最满意答案
下面是一个“切片”二维数组的一种方法的独立示意图:
Sub ArraySlicing() Dim arr(1 To 5, 1 To 5) Dim slice Dim x, y Dim a As Application 'Populate a sample 2-D array with values... For y = 1 To 5 For x = 1 To 5 arr(y, x) = "R" & y & ":C" & x Next x Next y '...done setting up sample array Set a = Application 'this is just to shorten the following lines 'Example 1: get the first "column" slice = a.Transpose(a.Index(arr, 0, 1)) Debug.Print Join(slice, ", ") 'display what we got 'Example 2: get second "row" (note double transpose) slice = a.Transpose(a.Transpose(a.Index(arr, 2, 0))) Debug.Print Join(slice, ", ") 'display what we got End SubIndex()给你一个二维数组 - (x,1)或(1,x) - Transpose()会将它转换为一维数组。
Here's a self-contained illustration of one way to "slice" a 2-D array:
Sub ArraySlicing() Dim arr(1 To 5, 1 To 5) Dim slice Dim x, y Dim a As Application 'Populate a sample 2-D array with values... For y = 1 To 5 For x = 1 To 5 arr(y, x) = "R" & y & ":C" & x Next x Next y '...done setting up sample array Set a = Application 'this is just to shorten the following lines 'Example 1: get the first "column" slice = a.Transpose(a.Index(arr, 0, 1)) Debug.Print Join(slice, ", ") 'display what we got 'Example 2: get second "row" (note double transpose) slice = a.Transpose(a.Transpose(a.Index(arr, 2, 0))) Debug.Print Join(slice, ", ") 'display what we got End SubIndex() gives you a 2-d array - (x,1) or (1,x) - Transpose() will convert that to a 1-d array.
更多推荐
发布评论