根据其ID快速访问MATLAB结构数组 [英] Fast access to a MATLAB array of structs based on its id
问题描述
我有一个Matlab结构:
I have a Matlab struct:
a(1).x=54.23; a(1).y=2.3; a(1).col=32.221; a(1).id=1;
a(2).x=5.23; a(2).y=3.3; a(2).col=2.221; a(2).id=2;
...等等.现在,我想访问ID为73的a
中的结构.我可以考虑进行for循环,但是事情是我必须基于id多次访问数组a
的元素. Wat是用于此目的的最快的数据结构吗?像字典一样的Python也许可以工作,但是我不确定要实现它.指出一些代码示例将非常有帮助.
... and so on. Now i want to access the struct in a
having id 73. I can think of doing a for loop but the thing is i have to access elements of array a
several times like this based on id. Wat is the fastest data structure available for this purpose? Python like dictionary may work but i am not sure ow to implement it. Pointing out some code examples would be very helpful.
推荐答案
尝试一下:
id=[a.id];
a(id==73)
它不像字典那样高效,但是如果它足够快达到您的目的,就不值得进一步研究.
It's not as efficient as a dictionary, but if it's fast enough for your purposes it's not worth looking further.
a.id
部分的计算结果是ID值的逗号分隔列表,这些ID值串联在一个数组中,然后可用于查找.
The a.id
part evaluates to a comma-separated list of id values, which are concatenated in an array that you can then use for lookup.
这篇关于根据其ID快速访问MATLAB结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!