这个answer州
I don't think you (or I) can do dynamic data structures 'in' MATLAB. We have to use MATLAB OO features and MATLAB classes. Since I think that these facilities are really a MATLAB wrapper around Java I make the bold claim that those facilities are outside MATLAB. A matter of semantics, I concede. If you want to do dynamic data structures with MATLAB, you have to use OO and classes, you can't do it with what I think of as the core language, which lacks pointers at the user level.
现在假设有一个袋子。新号码以随机顺序添加到袋子中,但号码仍应排序。数量未知。因此我需要一个动态数据结构:结构的大小必须能够改变。此外,结构必须能够保持平衡,即我需要对其进行排序。
Matlab 中的动态平衡数据结构要求应使用哪种数据结构?
最佳答案
Matlab 的矩阵本质上是动态的。如果您有一个有序数字的向量
,并且想要在适当的位置插入一个新的数字
(保持向量有序),您只需执行以下操作
[~, ind] = find(number<=vector,1,'first'); % determine where to insert
if isempty(ind), ind = numel(vector)+1; end % in this case, insert at the end
vector = [vector(1:ind-1) number vector(ind:end)]; % do the insert, extending the vector
当然,由于需要重新分配内存,这不是很快。
关于matlab - Matlab中的动态平衡数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493409/