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?

采纳的回答

Cris LaPierre
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)
r = 4
c = 1
When entering that as an (x,y) pair, you must use the column value for x, and the row value for y.

更多回答(0 个)

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by