如何使用八度创建和访问字典(或地图或映射)



如何用八度创建和访问词典(或映射或标记)?我已经尝试了几种方法,但是也许有更有效的方法。以下是显示我尝试过的代码。它使用此处提供的.mat文件的输入:https://www.dropbox.com/s/4v1z1q04ivpgjvf/sample_input_dictionary.mat?dl = 0

% Methods to build/use a dictionary on Octave
clear
tic
load sample_input_dictionary.mat;
pkg load general
toc
% Dict-based dictionary, using the "general" package: very slow to access
d_dict  = dict(nodes, num2cell(1:numel(nodes)));
toc
% Struct-based dictionary: slower to build, much faster to access
temp = [nodes', num2cell([1:numel(nodes)]')] .';
d_struct = struct(temp{:}); clear temp;
toc
% Is there an equivalent and more efficient cell-based dictionary?
% A different struct-based dictionary, which I could not build vectorially
t = struct();
for m=1:numel(nodes)
    t.(nodes{m}) = edges{m};
end
toc
% Example of accessing the above dictionaries
d_dict('1234') % this takes forever
d_struct.('1234')
t.('1234') 

您可以尝试 stundures 单元格数组 for Same。

我的偏爱是单元格数组。但是您尝试理解他们的工作并适当地决定。

如果您可以查看文档,那会更好。

我提供了一些可能有用的链接。

数据容器:链接到八度文档

结构:链接到八度文档

单元格数组:链接到八度文档

在版本4.4.0中添加了containers.Map数据结构。以下是编写此答案时的最新文档,版本6.2.0的链接。

https://octave.org/doc/v6.2.0/containers_002emap.html

最新更新