# Replacing values of a vector with values in another vector of a different size

5 visualizaciones (últimos 30 días)
Erin el 9 de Sept. de 2022
Comentada: Erin el 9 de Sept. de 2022
Hello,
Essentially I am trying to create a new matrix C that is the size of A, but replaces the 1s in A with the values in B while keeping everything in the same order. If the value in B is inf, I would like for it to be replaced by a 0.
Here is an example of what I am trying to do (the actual vectors are about 600 elements in length):
A = [ 0 0 0 1 1 1 0 0 1 1 0 0 0 ... ]
B = [ 1 2 3 inf 4 ... ]
C = [ 0 0 0 1 2 3 0 0 0 4 0 0 0 ... ]
Any ideas on how I can accomplish this?
Any help is much appreciated, thanks in advance!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Stephen23 el 9 de Sept. de 2022
Editada: Stephen23 el 9 de Sept. de 2022
A = [0,0,0,1,1,1,0,0,1,1,0,0,0];
B = [1,2,3,inf,4];
C = double(A);
C(A==1) = B;
C(isinf(C)) = 0
C = 1×13
0 0 0 1 2 3 0 0 0 4 0 0 0
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Erin el 9 de Sept. de 2022
Thank you so much!

Iniciar sesión para comentar.

### Categorías

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

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by