matlab - 加入数字向量的数字

标签 matlab

我是 Matlab 的新手,虽然不是编程。我正在尝试散列一个字符串,并取回一个值作为该字符串的唯一 ID。我正在使用 FileExchange 中的这个 DataHash 函数,它将散列作为整数向量返回。到目前为止,我找到的将其转换为单个数值的最佳解决方案是:

hash_opts.Format = 'uint8';
hash_vector = DataHash(string, hash_opts);
hash_string = num2str(hash_vector);
% Use a simple regex to remove all whitespace from the string,
% takes it from '1 2 3 4' to '1234'
hash_string = regexprep(hash_string, '[\s]', '');
hashcode = str2double(hash_string);

不依赖于 DataHash 的可重现示例:

hash_vector = [1, 23, 4, 567];
hash_string = num2str(hash_vector);
% Use a simple regex to remove all whitespace from the string,
% takes it from '1 2 3 4' to '1234'
hash_string = regexprep(hash_string, '[\s]', '');
hashcode = str2double(hash_string); % Output: 1234567

是否有更有效的方法来实现这一目标,而无需诉诸正则表达式?

最佳答案

是的,Matlab 的正则表达式实现不是特别快。我建议你使用 strrep :

hashcode = str2double(strrep(hash_string,' ',''));

或者,您可以使用不首先插入空格的字符串创建方法:

hash_vector = [1, 23, 4, 567];
hash_string = str2double(sprintf('%d',hash_vector))

只需确保您的哈希值小于 2^53 或 conversion to double might not be exact .

关于matlab - 加入数字向量的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16765231/

相关文章:

algorithm - 将点网络的 "paths"连接到 "routes"

python - Python 中的矩阵赋值

Matlab - 匿名函数中的for循环

matlab - 为什么 MATLAB 原生函数 cov(协方差矩阵计算)使用的除数与我预期的不同?

matlab - 如何在 MATLAB 中绘制带有标准偏差值的误差条形图?

重新排列数据集以创建具有滞后性的数据集(过去的观察)

algorithm - MATLAB中的相邻灰度相关性矩阵(NGLDM)

MATLAB 曲线拟合,指数与线性

matlab - .'* 运算符在 Matlab 中是什么意思?

database - 在 matlab 中创建多维 NetCDF