gpt4 book ai didi

c++ - Matlab 代码生成 : Does not support anonymous functions

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:45 25 4
gpt4 key购买 nike

我试图将一些 Matlab 代码更改为 C++,但是当我使用 %#codegen 时,错误“代码生成不支持匿名函数”出现在 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

这基本上与您的匿名用户实现的相同

关于c++ - Matlab 代码生成 : Does not support anonymous functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25820106/

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