Swapped row and column output from ind2sub function?
1 次查看(过去 30 天)
显示 更早的评论
I want to plot a matrix as an image and show a pixel value at corresponding point of an image. Here is a simple example.
Im0 = reshape(1:16, [4,4]);
figure; imagesc(Im0); daspect([1 1 1]); colorbar; hold on;
for ii = 1:numel(Im0)
[r,c] = ind2sub([4,4],ii);
text(r,c,num2str(Im0(ii))); hold on;
end
Clearly, this result is incorrect. However, if I swap the inputs in the 5th line:
text(c,r,num2str(Im0(ii))); hold on;
then I can get the desired result. I'm not sure why I needed to swap the inputs to the text function. Does the text function use a different coordinate frame?
0 个评论
采纳的回答
Cris LaPierre
2022-2-17
The issue here is that the inputs to text are x and y. You are using r and c, probably to indicate row and column. Just note that, for a matrix, row number corresponds to y location, and column corresponds to x location.
For example, the 4th item in linear indexing of your matrix is the bottom left block. This is the 4th row, 1st column.
[r,c] = ind2sub([4,4],4)
When entering that as an (x,y) pair, you must use the column value for x, and the row value for y.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!