matlab - Matlab中的动态平衡数据结构?

标签 matlab data-structures

这个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/

相关文章:

java - 基于不同的和第二个谓词的过滤器列表

matlab - 在不修改 Matlab 脚本的情况下禁用 Matlab 焦点窃取

MATLAB Mex C API 检查 mxarray 是否为基类

matlab - 在 MATLAB 中查找一个向量中的元素在另一个向量中的位置

algorithm - 寻找径向线段的最近邻居

matlab - 相当于 Octave/Matlab 中的 "apply"吗?

mysql - 2 个表的 INSERT 问题

algorithm - 我试图在时间 O(1) 的二叉搜索树中找到一个键的后继

c - 是否有任何具有通用数据结构的开源 C 库?

c - 将混合数据读入 C 结构