Replace some values of a vector with another vector which has a different size
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Christina
el 29 de Abr. de 2015
Comentada: Stephen23
el 29 de Abr. de 2015
Hello
I've got the vector: source = [ 0 0 1 0 1]
and the vector a = [2 3],
which has a different size to source.
I'd like to replace in the source vector the elements that are equal to 1, with those of vector a.
The final vector b should be: b = [0 0 2 0 3]
Any thoughts on this?
Thank you!
0 comentarios
Respuesta aceptada
Más respuestas (2)
pfb
el 29 de Abr. de 2015
b=zeros(size(source));
b(find(source))=a;
of course this works only if the number of nonzero elements in source are the same as the elements in a.
Stephen23
el 29 de Abr. de 2015
Editada: Stephen23
el 29 de Abr. de 2015
>> A = [ 0 0 1 0 1];
>> B = [ 2 3];
>> A(A==1) = B
A = 0 0 2 0 3
On my computer this was almost twice as fast compared to using find and zeros.
3 comentarios
James Tursa
el 29 de Abr. de 2015
Stephen switched nomenclature. His starting "A" is your "source" his "B" is your "a", and his resulting "A" is your "b".
Ver también
Categorías
Más información sobre Get Started with MATLAB 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!