how to shuffle two vectors?

What is the simplest way to shuffle two vectors? It is possible to do it in one line?
from u and v I want: [u(1),v(1),u(2),v(2),u(3),...]

 Respuesta aceptada

Thorsten
Thorsten el 12 de Oct. de 2015
Editada: Thorsten el 12 de Oct. de 2015

1 voto

u = rand(1,10);
v = 10*rand(1,10);
uv = [u; v]; uv = uv(:)';
or
n = numel(u); uv([1:2:2*n-1 2:2:2*n]) = [u v];

Más respuestas (1)

Walter Roberson
Walter Roberson el 12 de Oct. de 2015

0 votos

uv = [u(:).'; v(:).'];
uv = uv(:).';
If you are certain that u and v are row vectors then this can be simplified to
uv = [u; v];
uv = uv(:).';
It can also be written in a single line if you use reshape
uv = reshape([u; v], 1, []);

Categorías

Más información sobre Vehicle Dynamics Blockset en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Oct. de 2015

Respondida:

el 12 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by