MATLAB: How to do linear indexing in matlab

i am in urgent need

hi, Suppose I have a matrix A=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]; I want to transfer it into a linear matrix as A=[1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16]; (i.e. mortan scan order in image processing). Can you help me. All my work will start after this only. I would be very greatful to you

Best Answer

This will get you the desired matrix, whereas the other two don't (yet unless they edit them):
% Reference arrays.
A=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
desired = [1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16]
% Next two solutions don't produce desired.
B1 = A(:)'
B2 = reshape(reshape(A.',2,[]),1,[])
% This will get you the desired.
[rows, columns] = size(A)
B3 = [];
for row = 1 : 2 : rows
for col = 1 : 2 : columns
B3 = [B3, A(row, col), A(row, col+1), A(row+1, col), A(row+1, col+1)];
end
end
B3 % Report values to command window.