How can I do linear indexing in matlab?

3 visualizaciones (últimos 30 días)
TAUSIF
TAUSIF el 1 de Mzo. de 2014
Comentada: TAUSIF el 2 de Mzo. de 2014
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

Respuesta aceptada

Image Analyst
Image Analyst el 1 de Mzo. de 2014
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.
  3 comentarios
Image Analyst
Image Analyst el 1 de Mzo. de 2014
Editada: Image Analyst el 1 de Mzo. de 2014
I just copied and pasted his array and your code and I got this:
desired =
1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16
B2 =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
They looked different to me. My B3 gives this:
B3 =
1 2 5 6 3 4 7 8 9 10 13 14 11 12 15 16
which looks the same as what he desires his final out A to be.
TAUSIF
TAUSIF el 2 de Mzo. de 2014
The answer given by Image Analyst really works

Iniciar sesión para comentar.

Más respuestas (4)

Abdulrazzaq
Abdulrazzaq el 1 de Mzo. de 2014
Editada: Abdulrazzaq el 1 de Mzo. de 2014
try:
B = A(:)';
Best wishes
Abdulrazzaq

dpb
dpb el 1 de Mzo. de 2014
One way amongst many --
B=reshape(reshape(A.',2,[]),1,[]);
Consider how Matlab storage is in column-major order and what operation one needs to get to the desired organization from the original...

Matt J
Matt J el 1 de Mzo. de 2014
Editada: Matt J el 1 de Mzo. de 2014
Using MAT2TILES (available here),
B=cellfun(@(c) c(:).', mat2tiles(A.',[2,2]),'uni',0);
B=[B{:}]

Roger Stafford
Roger Stafford el 1 de Mzo. de 2014
If you convert each of the integers 0:15 to binary, permute the digits appropriately, recalculate the numbers from these permuted digits, and finally add 1 to each, you will get the correct linear indexing. The same holds true for larger Z-orders.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by