## How to extract column and row index and the corresponding value from a matrix?

### Vivaswan ChandraShekar (view profile)

on 23 Jun 2019
Latest activity Commented on by Andrei Bobrov

### Andrei Bobrov (view profile)

on 26 Jun 2019
I have a huge matrix of pixel values from an image converted into a text file. I need to convert this matrix into a table with column index, row index and the element as seperate columns of the table. How do I do this?
I'm doing this to figure out of the Centre of mass of a 2D projection of an object in the image. The image is a binary image with a single object in white and the background in black. Is there any other way to do this?

on 23 Jun 2019
A sample illustration would help
Vivaswan ChandraShekar

### Vivaswan ChandraShekar (view profile)

on 23 Jun 2019
Like this:
from -
0 1 4
7 6 0
to -
row column element
1 1 0
1 2 1
1 3 4
2 1 7
2 2 6
2 3 0

### Release

R2019a

on 23 Jun 2019

on 23 Jun 2019

m = [0 1 4
7 6 0];
[Rows,Columns] = ind2sub(size(m),(1:numel(m))');
Wanted = [Rows,Columns,m(:)];
Wantedtable = sortrows(array2table(Wanted,'VariableNames',{'Rows','Columns','Elements'}),1)

KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 23 Jun 2019
@Madhan I tried, could not implement it without loop. +1

### Andrei Bobrov (view profile)

on 23 Jun 2019

A = [0 1 4
7 6 0];
out = sortrows([fullfact(size(A)),A(:)]);

KALYAN ACHARJYA

on 24 Jun 2019
It's great!
Andrei Bobrov

### Andrei Bobrov (view profile)

on 26 Jun 2019
Thank you Kalyan!