在 MATLAB 中通过正则表达式匹配向量化变量创建

发布于 2024-10-26 03:13:24 字数 1036 浏览 3 评论 0原文

是否可以对下面的循环进行矢量化?

% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风启觞 2024-11-02 03:13:24

它并不完全是矢量化,只有当您的目的是从头开始创建字段而不是更新现有结构时才有效,但是:

fields = struct(splits{:});

It's not exactly vectorizing and only works if your intention is to create fields from scratch rather than updating an existing struct, but:

fields = struct(splits{:});
落日海湾 2024-11-02 03:13:24

您可以使用 CELL2STRUCT

cell2struct(splits(2:2:end),splits(1:2:end),2)
ans = 
    cubeId: '001000'
         X: '10'
         Y: '10'
         Z: '10'
      minX: '8590'
      maxX: '9200'
      minY: '8590'
      maxY: '9200'
      minZ: '87'
      maxZ: '95'

如果您想让字段包含数字,您可以可以改为写

cell2struct(cellfun(@str2double,splits(2:2:end),'uniformOutput',false),splits(1:2:end),2)

另外,您可以修改正则表达式,以便它立即返回一个结构(抱歉,我没有输入整个表达式):

regexp(parseStr,'cubeId=(?<cubeId>\d+)_X=(?<X>\d+)','names')
ans = 
    cubeId: '001000'
         X: '10'

You can use CELL2STRUCT

cell2struct(splits(2:2:end),splits(1:2:end),2)
ans = 
    cubeId: '001000'
         X: '10'
         Y: '10'
         Z: '10'
      minX: '8590'
      maxX: '9200'
      minY: '8590'
      maxY: '9200'
      minZ: '87'
      maxZ: '95'

If you want to have the fields contain numbers, you can instead write

cell2struct(cellfun(@str2double,splits(2:2:end),'uniformOutput',false),splits(1:2:end),2)

Additionally, you can modify your regular expression, so that it returns a struct right away (I didn't type up the entire expression, sorry):

regexp(parseStr,'cubeId=(?<cubeId>\d+)_X=(?<X>\d+)','names')
ans = 
    cubeId: '001000'
         X: '10'
丘比特射中我 2024-11-02 03:13:24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文