我怎样才能在MATLAB中为这个矢量生成一个映射关键字? [英] How can I generate a map key for this vector in MATLAB?
问题描述
我有一个功能正在查看一些元素。每个元素都是8x1列向量的形式。向量中的每个条目都是一个小于1000的整数。每次我看到这样一个向量时,我想在检查该向量不在该列表中之后,将其添加到已经看过向量的列表中。该函数将检查大约100,000个这样的向量。
原来我试过用 ismember(v',M,'rows')
,但发现这很慢。下一步,我试过:
found = containers.Map('KeyType','double','ValueType','any') ;
然后每次检查一个新的向量 v
,计算:
key = dot(v,[1000000000000000000000 1000000000000000000 1000000000000000 ...
1000000000000 1000000000 1000000 1000 1 ]);
然后检查 isKey(found,key)
。如果钥匙不在容器中,那么 found(key)= 1
。
这看起来像一个漂亮糟糕的解决方案,尽管它的运行速度远远超过 ismember
。任何帮助/建议将不胜感激。编辑:也许它会更好地使用 mat2str
来生成关键,而不是这个愚蠢的点产品?
最简单的方法来生成一个键/使用
char将整数值的向量转换为字符数组
。由于您的整数值永远不会超过1000,并且 char
可以接受0到65535之间的数字值(对应于Unicode字符),所以这会为您提供一个唯一的8字符密钥每一个独特的8乘1向量。下面是一个例子: found = containers.Map('KeyType','char','ValueType','any') ;
v = randi(1000,[8 1]); %示例向量
key = char(v);
if〜isKey(found,key)
found(key)= 1;
end
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 time I see such a vector, I'd like to add it to a list of "already seen" vectors, after checking to see that the vector is not already on this list. The function will examine on the order of ~100,000 such vectors.
Originally I tried using ismember(v', M, 'rows')
, but found this to be very slow. Next I tried:
found = containers.Map('KeyType', 'double', 'ValueType', 'any');
Then each time I examine a new vector v
, compute:
key = dot(v, [1000000000000000000000 1000000000000000000 1000000000000000 ...
1000000000000 1000000000 1000000 1000 1]);
Then check isKey(found, key)
. If the key is not in the container, then found(key) = 1
.
This seems like a pretty lousy solution, even though it does run considerably faster than ismember
. Any help/suggestions would be greatly appreciated.
EDIT: Perhaps it would be better to use mat2str
to generate the key, rather than this silly dot product?
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
这篇关于我怎样才能在MATLAB中为这个矢量生成一个映射关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!