Tuple structure equivalent in Matlab

雨燕双飞 提交于 2019-12-12 01:46:58

问题


In Haskell, there is a structure called 'tuples' which allows two elements to be paired together (Ie: (1,2), ('A', 'B') etc)

I was wondering if there was something similar in Matlab so that I could match elements and then query matlab in a way similar to "If element X is matched to Y then.. else.."

Thanks!


回答1:


The closet thing I know of in MATLAB is to use a map object. They are pretty easy to use. You can create one as follows

someMap = containers.Map();

Adding a new key is pretty easy as well

someMap('someKey') = 'someValue';

The key needs to be a string by default, but this can be edited. You can also check if the key exists already by calling

someMap.isKey('someKey')

And values are accessed by just calling

someMap('someKey')

This should mimic the behavior that you are looking for. You can always read more by looking at the documentation. containers.Map




回答2:


A simple cell array may be able to do the trick for you:

C = {1, 2; 'A' 'B'}

You can now easily perform tests on it, for example:

strcmp(C, 'A')


来源:https://stackoverflow.com/questions/20378121/tuple-structure-equivalent-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!