我正在尝试将一些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
感谢任何能帮助我理解这一点的人。
答案 0 :(得分:4)
首先,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);
),所以你将得到全部为零的结果。尝试将其初始化为实际图像或至少随机整数。
干杯!