How do I get the x y coordinates from an index?

If I have a matrix
A =[0.1 0.2 0.3
0.4 0.5 0.6
0.7 0.8 0.9];
And i have index 4, how do I get the x and y values for index 4.
In this case I want to get x = 2 y =1.

 Accepted Answer

My best guess as to what you want —
A =[0.1 0.2 0.3
0.4 0.5 0.6
0.7 0.8 0.9];
index = 4;
[r,c] = ind2sub(size(A),index)
r = 1
c = 2
See the documentation on ind2sub for details.
.

More Answers (0)

Asked:

on 19 May 2021

Commented:

on 20 May 2021

Community Treasure Hunt

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

Start Hunting!