簡體   English   中英

Matlab Codegen:不支持匿名函數

[英]Matlab Codegen: Does not support anonymous functions

我試圖將一些 Matlab 代碼更改為 C++,但是當我使用 %#codegen 時,錯誤“代碼生成不支持匿名函數”出現在vec = @(x) x(:);旁邊vec = @(x) x(:); . 下面是 Matlab 函數。 我可以更改什么來消除此錯誤?

    function [L,num,sz] = label(I,n) %#codegen

    % Check input arguments
    error(nargchk(1,2,nargin));
    if nargin==1, n=8; end

    assert(ndims(I)==2,'The input I must be a 2-D array')

    sizI = size(I);
    id = reshape(1:prod(sizI),sizI);
    sz = ones(sizI);

    % Indexes of the adjacent pixels
    vec = @(x) x(:);
    if n==4 % 4-connected neighborhood
    idx1 = [vec(id(:,1:end-1)); vec(id(1:end-1,:))];
    idx2 = [vec(id(:,2:end)); vec(id(2:end,:))];
    elseif n==8 % 8-connected neighborhood
    idx1 = [vec(id(:,1:end-1)); vec(id(1:end-1,:))];
    idx2 = [vec(id(:,2:end)); vec(id(2:end,:))];
    idx1 = [idx1; vec(id(1:end-1,1:end-1)); vec(id(2:end,1:end-1))];
    idx2 = [idx2; vec(id(2:end,2:end)); vec(id(1:end-1,2:end))];
    else
    error('The second input argument must be either 4 or 8.')
    end

    % Create the groups and merge them (Union/Find Algorithm)
   for k = 1:length(idx1)
   root1 = idx1(k);
   root2 = idx2(k);

   while root1~=id(root1)
    id(root1) = id(id(root1));
    root1 = id(root1);
   end
   while root2~=id(root2)
    id(root2) = id(id(root2));
    root2 = id(root2);
   end

   if root1==root2, continue, end
   % (The two pixels belong to the same group)

   N1 = sz(root1); % size of the group belonging to root1
   N2 = sz(root2); % size of the group belonging to root2

   if I(root1)==I(root2) % then merge the two groups
    if N1 < N2
        id(root1) = root2;
        sz(root2) = N1+N2;
    else
        id(root2) = root1;
        sz(root1) = N1+N2;
    end
    end
    end

    while 1
    id0 = id;
    id = id(id);
    if isequal(id0,id), break, end
    end
    sz = sz(id);

    % Label matrix
    isNaNI = isnan(I);
    id(isNaNI) = NaN;
    [id,m,n] = unique(id);
    I = 1:length(id);
    L = reshape(I(n),sizI);
    L(isNaNI) = 0;

    if nargout>1, num = nnz(~isnan(id)); end 

您可以使用以下功能並注釋匿名功能

function x=vec( id )
    x = id(:);
end

這基本上與您的匿名用戶所實現的相同

MATLAB Coder 自 R2016b 起支持匿名函數。 上面的示例適用於最新版本的 MATLAB Coder(已於 2021 年 11 月 30 日檢查)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM