How can I generate a map key for this vector in MATLAB?

前端 未结 3 1664
名媛妹妹
名媛妹妹 2021-01-18 23:39

I have a function that is looking at a number of elements. Each element is of the form of an 8x1 column vector. Each entry in the vector is an integer less than 1000. Every

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 00:32

    The easiest way to generate a key/hash in your case would be to just convert the vector of integer values to a character array using char. Since your integer values never go above 1000, and char can accept numeric values from 0 to 65535 (corresponding to Unicode characters), this will give you a unique 8-character key for every unique 8-by-1 vector. Here's an example:

    found = containers.Map('KeyType', 'char', 'ValueType', 'any');
    
    v = randi(1000, [8 1]);  % Sample vector
    key = char(v);
    if ~isKey(found, key)
      found(key) = 1;
    end
    

提交回复
热议问题