I am trying to convert some C+开发者_开发问答+ code to MATLAB and having an issue. Would someone please explain a single line of code from below.
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]];
}
}
Could someone explain from
diff = diff || (image[itt] != image[image[itt]]);
image[itt] = image[image[itt]];
This part
image[image[itt]]
I don't understand how this works in terms of 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
Thanks to anyone who can help me understand this.
First of all, ImageSize = (Image);
is not correct. This will simply assign Image
to ImageSize
. Use ImageSize = size(Image);
instead.
Secondly, you are using double indexing in you Matlab code, while the C++ code is using single indexing.
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
You might have to transpose your matrix, because I'm not certain whether the C++ code operates column-wise or row-wise. Also, because you're initializing you matrix to all zeros (zeros(100,100);
), you're going to get all zeros back as the result. Try initializing it to an actual image or at least random integers.
Cheers!
精彩评论