在 MATLAB 中通过正则表达式匹配向量化变量创建
是否可以对下面的循环进行矢量化?
% String to parse (we want to create variables that are defined implicitly in the string):
parseStr = 'cubeId=001000_X=10_Y=10_Z=10_minX=8590_maxX=9200_minY=8590_maxY=9200_minZ=87_maxZ=95';
% Splitting string for regexp
matchStr = '=|_';
[start_idx, end_idx, extents, matches, tokens, names, splits] = ...
regexp(parseStr, matchStr);
% Inspecting the splits
>> splits
splits =
Columns 1 through 8
'cubeId' '001000' 'X' '10' 'Y' '10' 'Z' '10'
Columns 9 through 15
'minX' '8590' 'maxX' '9200' 'minY' '8590' 'maxY'
Columns 16 through 20
'9200' 'minZ' '87' 'maxZ' '95'
% Loop that we are interested in vectorizing:
for ix = 1:2:numel(splits)
fields.(splits{ix}) = splits{ix+1};
end
% Result:
>>fields
fields =
cubeId: '000900'
X: '10'
Y: '9'
Z: '10'
minX: '8590'
maxX: '9200'
minY: '7590'
maxY: '8610'
minZ: '87'
maxZ: '95'
Is it possible to vectorize the loop below?
% String to parse (we want to create variables that are defined implicitly in the string):
parseStr = 'cubeId=001000_X=10_Y=10_Z=10_minX=8590_maxX=9200_minY=8590_maxY=9200_minZ=87_maxZ=95';
% Splitting string for regexp
matchStr = '=|_';
[start_idx, end_idx, extents, matches, tokens, names, splits] = ...
regexp(parseStr, matchStr);
% Inspecting the splits
>> splits
splits =
Columns 1 through 8
'cubeId' '001000' 'X' '10' 'Y' '10' 'Z' '10'
Columns 9 through 15
'minX' '8590' 'maxX' '9200' 'minY' '8590' 'maxY'
Columns 16 through 20
'9200' 'minZ' '87' 'maxZ' '95'
% Loop that we are interested in vectorizing:
for ix = 1:2:numel(splits)
fields.(splits{ix}) = splits{ix+1};
end
% Result:
>>fields
fields =
cubeId: '000900'
X: '10'
Y: '9'
Z: '10'
minX: '8590'
maxX: '9200'
minY: '7590'
maxY: '8610'
minZ: '87'
maxZ: '95'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它并不完全是矢量化,只有当您的目的是从头开始创建字段而不是更新现有结构时才有效,但是:
It's not exactly vectorizing and only works if your intention is to create
fields
from scratch rather than updating an existing struct, but:您可以使用 CELL2STRUCT
如果您想让字段包含数字,您可以可以改为写
另外,您可以修改正则表达式,以便它立即返回一个结构(抱歉,我没有输入整个表达式):
You can use CELL2STRUCT
If you want to have the fields contain numbers, you can instead write
Additionally, you can modify your regular expression, so that it returns a struct right away (I didn't type up the entire expression, sorry):
Cellfun 不会矢量化您的代码。相反,它有一个内部循环来遍历参数。
上面的代码无法矢量化,因为它是一个字符串列表。不管怎样你都必须在某个时候调用一些for循环。
事实上,调用 cell2struct 的 cellfun 解决方案速度较慢,因为您需要在函数之间进行提取调用。
在我的机器上,for 循环最多需要 0.0008 秒,而 cellfun 解决方案最多需要 0.0015 秒。
Cellfun does not vectorise your code. Instead it has an internal loop that goes through the arguments.
The above code cannot be vectorised because it is a list of strings. No matter what you will have to call at some point some for loop.
In fact the cellfun solution with a call to cell2struct is slower because you need to make extract calls between functions.
On my machine the for loop requires 0.0008 seconds at most and the cellfun solution 0.0015sec at best.