使用变量跟踪大小。
我需要了解更多关于您的用法以给出具体答案,但请考虑以下事项:
Preallocate max space for mem
While looping over data sets
Set k = 1
While looping over data set elements
Add element to mem(k)
Set k = k + 1
End
Extract the data you want with mem(1:k-1)
Use the extracted data
EndMatlab喜欢预先分配的数据,因为通常情况下,您可以知道您的设置有多大。
While looping over data sets
Determine size of data set, and preallocate mem here with that size
Set k = 1
While looping over data set elements
Add element to mem(k)
Set k = k + 1
End
mem already has exactly the data you need, so begin using it
End当然,我们更喜欢向量命令来摆脱循环和计数器变量:
While looping over data sets
Use vector calculation with only the input you need to produce only the output you need
mem already has exactly the data you need, so begin using it
End