问题
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