gpt4 book ai didi

matlab - 加入数字向量的数字

转载 作者:太空宇宙 更新时间:2023-11-03 20:12:03 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com