Matlab中的动态平衡数据结构? [英] Dynamic balanced data structure in Matlab?

查看:129
本文介绍了Matlab中的动态平衡数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答指出


我不认为你(或我)可以在MATLAB中做动态数据结构。
我们必须使用MATLAB OO特性和MATLAB类。由于我认为
这些设施实际上是一个围绕Java的MATLAB包装器,所以我们大胆地声称这些设施在MATLAB之外。一个
语义的问题,我承认。如果你想用
MATLAB做动态数据结构,你必须使用OO和类,你不能用我的
认为是核心语言,在用户级别缺少指针。

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.

现在假设一个包。新的号码以随机顺序添加到包中,并且仍然应该对数字进行排序。数字的数量是未知数。因此,我需要一个动态的数据结构:结构的大小必须能够被改变。此外,结构必须能够获得平衡,即我需要得到它的订购。

Now suppose a bag. New numbers are added to the bag in random order and still the numbers should be ordered. The amount of numbers is unknown. Hence I need a dynamic data-structure: the size of the structure must be able to get changed. Also the structure must be able to get balanced i.e. I need to get it ordered.

Matlab中的动态平衡数据结构要求应该使用哪种数据结构?

Which data structure should I use for the dynamic balanced data-structure requirement in Matlab?

推荐答案

Matlab的矩阵本质上是动态的。如果您有一个矢量的有序数字,并希望在其正确的位置插入一个新的数字 ),你可以简单地做

Matlab's matrices are inherently dynamic. If you have a vector of ordered numbers and want to insert a new number in its proper place (maintaining the vector ordered), you can simply do

[~, 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

当然这不是很快,因为需要内存重新分配。

Of course this is not very fast because of the need for memory reallocation.

这篇关于Matlab中的动态平衡数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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