问题描述
我想从一个二维数组创建一维数组,而不需要循环使用1行代码,如:
newvec = oldvec(:,3)
在MATLAB中创建一个1维数组newvec oldvec栏目。我的搜索告诉我在VBA中这样做的唯一方法是循环。例如:
$ $ $ $ $ $ c $ red $ newvec(ubound(oldvec,1))
for i = 1 to ubound(oldvec, 1)
newvec(i)= oldvec(i,3)
next i
是否有一个内置的构造,用于剥离现有二维数组的整个奇异维以构建一个新的一维数组?
与大多数常见的编程语言不同,VBA中没有用于此目的的内建函数。但是有一个解决方法,使用索引函数:
pre $ Application_Index(MultidimArray,Row_Number,Column_Number)
$为了从源数组中提取一列,'0'应该作为
行号来传递论据。类似地,为了从源数组中提取一行,
'0'应该作为column_number参数传递。
想从oldvec的第三列创建一个1维数组newvec:
$ b $ pre $ new $ c $ ApplicationIndex(oldvec, 0,3)
你可以找到更多。
$ b
编辑:
使用 For循环
比使用索引函数
要快得多。所以你最好坚持 for循环
。详情请参阅评论。
I want to create a 1D array from a 2D array without looping using only 1 line of code such as:
newvec = oldvec(:,3)
which in MATLAB would create a 1D array "newvec" from the 3rd column of "oldvec". My searching tells me the only way to do this in VBA is looping. For example:
redim newvec(ubound(oldvec,1))
for i = 1 to ubound(oldvec,1)
newvec(i) = oldvec(i,3)
next i
Is there a built in construct for stripping our entire singular dimensions of an existing 2D array to build a new 1D array?
There is no builtin function in VBA for this purpose unlike most common programming languages. However there is a workaround using Index function:
Application.Index(MultidimArray, Row_Number, Column_Number)
So if you want to create a 1D array "newvec" from the 3rd column of "oldvec":
newvec = Application.Index(oldvec, 0, 3)
Here you can find more.
Edit:
Using For loop
is a lot faster than using Index function
. So you better stick with for loop
. See comments for details.
这篇关于剥离列或行来制作一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!