Swapped row and column output from ind2sub function?

2 vues (au cours des 30 derniers jours)
Daigo
Daigo le 17 Fév 2022
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?

Réponse acceptée

Cris LaPierre
Cris LaPierre le 17 Fév 2022
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.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by