容器映射和matlab中的结构体之间的区别 [英] The difference between a container map and a struct in matlab

查看:135
本文介绍了容器映射和matlab中的结构体之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解在matlab中的容器映射和结构体之间的区别。



从我看过来看,容器映射的优点它可以使用任何数字或字符串作为键,而struct字段只能使用合法变量名称的字符串(例如,它不会接受 mystruct。('123string')



在结构体上使用容器地图是否还有其他优势?



谢谢

解决方案

容器图和结构体背后的概念是完全不同的:



容器映射用于创建映射或索引。例如,如果您有一个矩阵 A ,并希望对第二行和第二列中的元素进行索引,则可以使用 A(2, 2)。对于数字,矩阵等,您要在哪里索引特定的行号,这很方便。然而,假设您有一个情况,如以下示例所示:



这里您有一系列值,您可以索引为1月份的 value(1),依此类推。但是这不是很清楚。如果您可以使用值('Jan')进行索引,那将会更加方便。这正是容器地图提供的。正如@marsei在他的评论中所说,容器映射是一个基于Java的无序结构,它使用哈希表进行索引。



结构是一个不同的结构,这是基于C和订购(感谢insignt @marsei)。 struct的主要用途是帮助您以更逻辑的方式存储数据。例如,当使用图像时,您通常有两个变量:一个用于图像数据,另一个用于颜色图。没有结构体,您需要在工作区中保留这两个单独的变量。对于多个图像,这会变得非常混乱(例如使用名称,如 img0_data img0_map 等等)。一个结构可以帮助你以一种简单的方式来组织它:一个名为 img0 的结构与数据地图



在词典的上下文中,两种构造或多或少相同,尽管结构通常似乎比容器映射更快。另外,正如问题中已经提到的,容器映射的可以是任何单个值,而对于结构体,它必须是一个字符串。


I would like to understand the difference between container map and a struct in matlab.

From what I've looked, it seems that the advantages of a container map is that it can use any number or string as a key, while a struct field can only take strings that are legit variable names (e.g. it won't accept mystruct.('123string'))

Are there any other advantages for using a container map over a struct?

Thanks

解决方案

The concepts behind a container map and a struct are quite different:

A container map is used to create a mapping or indexing. For example if you have a matrix A and want to index the element in the second row and second column, you will index it using A(2,2). For numbers, matrices and so on, where you want to index a specific row number, this is convenient. Assume however, you have a situation as shown in the following example by Mathworks:

Here you have an array of values, which you could index as value(1) for January, and so on. However this is not very legible. It would be much more convenient if you could index it with value('Jan'). This is exactly what container maps offer. As @marsei remarked in his comment, container maps are a Java-based, unordered, construct, that uses hash-tables for indexing.

A struct is a different construct, which is C-based and ordered (thanks for the insignt @marsei). The main use of struct is to help you store data in a more logical way. For example when using images, you often have two variables: one for the image data, and one for the color map. Without structs, you need to keep those two separate variables in the workspace. For multiple images this gets very confusing (e.g. using names like img0_data, img0_map and so on). A struct helps you organize this in a simple way: A struct called img0 with the fields data and map.

In the context of dictionaries, both constructs are more or less equivalent, though structs usually seem to be faster than container maps. Plus, as already mentioned in the question, the key of a container map can be any single value, while for structs it has to be a string.

这篇关于容器映射和matlab中的结构体之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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