如何使用Octave创建和访问字典(或映射或散列表)
问题描述:
如何使用Octave创建和访问字典(或映射或散列表)? 我尝试了几种方法,但也许有更有效的方法。下面是显示我尝试过的代码。它使用.MAT文件此处提供的输入: https://www.dropbox.com/s/4v1z1q04ivpgjvf/sample_input_dictionary.mat?dl=0如何使用Octave创建和访问字典(或映射或散列表)
% 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')
答
您可以尝试结构或电池阵列的一样。
我的偏好是针对单元阵列。但是你试着理解他们做了什么并且做出适当的决定。
如果你能看看文档,那会更好。
我提供了一些可能有用的链接。
数据容器:link to Octave Documentation
+0
非常感谢。我已经想出了如何使用结构作为字典,虽然还没有完全掌握这种用法,特别是如何矢量化装订字典。至于单元格,请问我可以使用我编辑的问题中提供的例子来向我展示如何使用它们来构建字典,如果效率更高? – highalpha
请出示一些代码,到目前为止,你已经尝试过的东西,创造MCVE https://stackoverflow.com/帮助/ mcve – Andy
感谢您指出。我已经添加了代码。 – highalpha