convert array A values to indices of array B

10 visualizaciones (últimos 30 días)
mb
mb el 30 de En. de 2015
Comentada: mb el 30 de En. de 2015
hi,
lets say I have a 10x10 array A with (4,4) = -2 and (6,6) = 2 . How would I construct an array B such that these values mean the shift in horizontal direction, e.g array B is zeros, except B(4,2) = 2 and B(6,8) = -2 ?
many thanks!

Respuesta aceptada

Guillaume
Guillaume el 30 de En. de 2015
Possibly, this is what you want, although it's not clear where the values to go in B come from:
A = zeros(10);
A(4, 4) = -2;
A(6, 6) = 2;
[r, c] = find(A); %find original coordinates of non zero values
c = c + nonzeros(A); %shift by value at coordinates
B = zeros(size(A));
B(sub2ind(size(B), r, c)) = -nonzeros(A) %are the values just the negative of the original ones?
  1 comentario
mb
mb el 30 de En. de 2015
again Guillaume,
you saved my day!
many thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by