Swapped row and column output from ind2sub function?

1 回表示 (過去 30 日間)
Daigo
Daigo 2022 年 2 月 17 日
回答済み: Cris LaPierre 2022 年 2 月 17 日
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