gpt4 book ai didi

c++ - 将 C++ 转换为 MATLAB

转载 作者:搜寻专家 更新时间:2023-10-31 01:19:49 25 4
gpt4 key购买 nike

我正在尝试将一些 C++ 代码转换为 MATLAB,但遇到了问题。请有人从下面解释一行代码。

  int *image      = (int *) malloc(size*sizeof(int)) ;

bool diff = true;
while (diff)
{
diff = false;
for (unsigned int itt = 0; itt < size; itt++)
{
diff = diff || (image[itt] != image[image[itt]]);
image[itt] = image[image[itt]];
}
}

谁能解释一下

  diff = diff || (image[itt] != image[image[itt]]);
image[itt] = image[image[itt]];

这部分

image[image[itt]]

我不明白这在 MATLAB 方面是如何工作的

Image = zeros(100,100);

ImageSize = (Image);
diff = true;

while(diff)
diff = false;
for x=1:ImageSize(1) % Height
for y=1:ImageSize(2) % Width
diff = diff || (image(x,y) ~= ????);
image(x,y) = ????;
end
end
end

感谢任何能帮助我理解这一点的人。

最佳答案

首先,ImageSize = (Image); 是不正确的。这将简单地将 Image 分配给 ImageSize。请改用 ImageSize = size(Image);

其次,您在 Matlab 代码中使用双索引,而 C++ 代码使用单索引。

while(diff)
diff = false;
for n=1:numel(Image) %# Total pixels in the image
diff = diff || (Image(n) ~= Image(Image(n)));
Image(n) = Image(Image(n));
end
end

您可能需要转置矩阵,因为我不确定 C++ 代码是按列还是按行操作。此外,因为您将矩阵初始化为全零 (zeros(100,100);),所以您将返回全零作为结果。尝试将其初始化为实际图像或至少随机整数。

干杯!

关于c++ - 将 C++ 转换为 MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5651568/

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