Insert zeros in a vector
Mostrar comentarios más antiguos
Let's assume my vector is
A = [1 2 3 7 9 10 45 93 122 150]';
I want to put zeros in between these numbers.
A = [1 2 3 0 0 0 7 0 9 10 0 0 .....]';
Respuesta aceptada
Más respuestas (1)
A = [1 2 3 7 9 10 45 93 122 150]';
B = zeros(150, 1);
Index = [true(1, numel(A)), false(1, numel(B) - numel(A)];
Index = Index(randperm(numel(Index));
B(Index) = A;
Now the result is filled at random places by zeros such that the total length is 150.
[EDITED] Easier:
A = [1 2 3 7 9 10 45 93 122 150]';
B = zeros(150, 1);
Index = randperm(numel(B), numel(A));
B(Index) = A;
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!