MATLAB: I have a matrix like [1 0 0 1 0 1 0 ; 0 1 0 1 0 0 ; 1 0 0 1 1 1]. there are 3 rows and 7 column i wanna to see in the format like

matrix conversion into another format

i have a matrix like [1 0 0 1 0 1 0 ; 0 1 0 1 0 0 ; 1 0 0 1 1 1]. there are 3 rows and 7 column i wanna to see in the format like [ 1 1 1; 1 2 0; 1 3 0; 1 4 1; 1 5 0 ; 1 6 1; 1 7 0; 2 1 ; 2 2…;]

Best Answer

>> M = [1 0 0 1 0 1 ; 0 1 0 1 0 0 ; 1 0 0 1 1 1]
M =
1 0 0 1 0 1
0 1 0 1 0 0
1 0 0 1 1 1
>> S = size(M);
>> [R,C] = ndgrid(1:S(1),1:S(2));
>> sortrows([R(:),C(:),M(:)])
ans =
1 1 1
1 2 0
1 3 0
1 4 1
1 5 0
1 6 1
2 1 0
2 2 1
2 3 0
2 4 1
2 5 0
2 6 0
3 1 1
3 2 0
3 3 0
3 4 1
3 5 1
3 6 1