Relocate a value of a vector.

1 visualización (últimos 30 días)
KonVak
KonVak el 17 de En. de 2020
Editada: Matt J el 17 de En. de 2020
Hello, i need to relocate a random value of a vector to a random position in it without changing the order of the other numbers.
I already have achieved finding a random value and its position and also the desired position.
It should look like this , for example
A=[1 2 3 4 5 6 7 8 9 0]
Position 7 to position 2
A=[1 7 2 3 4 5 6 8 9 0]
and it should also work the other way , position 2 to 7 since the positions are random.

Respuesta aceptada

Matt J
Matt J el 17 de En. de 2020
Editada: Matt J el 17 de En. de 2020
A=[1 2 3 4 5 6 7 8 9 0];
i=2; %position removed
j=7; %target position
B=A;
k=i<j;
B(i)=inf;
C = [B(1:j+k-1),A(i),B(j+k:end)];
result=C(isfinite(C))
>> result =
1 7 2 3 4 5 6 8 9 0

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by