MATLAB:在不处理整个数组的情况下将行追加到数组 [英] MATLAB: Append row to an array without processing the entire array

查看:346
本文介绍了MATLAB:在不处理整个数组的情况下将行追加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以1000Hz的频率在眼睛跟踪系统上运行解决方案.数据以X和Y凝视位置坐标的形式从眼动仪输入.我正在尝试将这两个坐标添加到数组的末尾.

我当前的解决方法如下:

gazePositionArray = [];    
while gazeDataIsAvailable
    [x y] = getNewCoordinates;
    gazePositionArray = [gazePositionArray; x y];
end

问题出在第四行.为了更新数组,它会复制旧数组的内容,并在末尾添加新坐标.例如,在录制的前30秒就可以了,但是阵列中存在的数据越多,计算机创建新阵列所要做的处理就越多(30秒后,系统尝试复制30,000.每秒1000行数据-每次迭代越来越多)

有没有一种方法可以将行追加到MATLAB数组的末尾而无需系统将整个数组复制到自身中?

解决方案

一些相关问题:

在MATLAB中长度未知的矩阵?

在MATLABàstd中预分配内存:: vector :: reserve(n).


典型的解决方案是预分配大"内存,并在需要时智能地复制到更大的内存.我最喜欢的是在当前分配已满时将大小增加一倍,但是还有其他分配.

没有什么困难的,但是它们都需要一些额外的代码行.有关示例,请参见上面的问题和答案.

I am running a solution on an eye tracking system at 1000Hz. Data comes in from the eye tracker in the form of a X and Y gaze position co-ordinate. I am trying to add these two co-ordinates to the end of an array as they come in.

My current solution is as follows:

gazePositionArray = [];    
while gazeDataIsAvailable
    [x y] = getNewCoordinates;
    gazePositionArray = [gazePositionArray; x y];
end

The problem lies in the fourth line. In order for the array to update, it copies the contents of the old array and adds the new co-ordinates on the end. This is fine, for example, for the first 30 seconds of recording, but the more data exists in the array, the more processing the computer has to do in order to create the new array (after 30 seconds the system is trying to copy 30,000 lines of data 1000 times per second - with this getting more and more at each iteration)

Is there a way to append a row to the end of a MATLAB array without the system having to copy the whole array into itself?

解决方案

Some related questions:

Matrix of unknown length in MATLAB?

Pre-allocating memory in MATLAB à la std::vector::reserve(n).


The typical solution is to preallocate a "big" piece of memory, and the smartly copy to a bigger piece of memory when needed. My favorite is to double the size whenever the current allocation become full, but there are others.

None are hard, but they all require a few extra lines of code. See the questions and answer above for some examples.

这篇关于MATLAB:在不处理整个数组的情况下将行追加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆