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;
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?
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.