Borrar filtros
Borrar filtros

Inserting zeros into another array at values in the second array

2 visualizaciones (últimos 30 días)
mark palmer
mark palmer el 24 de Nov. de 2023
Respondida: Image Analyst el 24 de Nov. de 2023
Hi, Lets have the following arrays:
A = [8 2 3 5 5 6 7 8 9];
B = [1 3 2 3]
Now, I want to write a code which can insert zeros into A after every location in B. So, my final output would be
F = [8 0 2 3 5 0 5 6 0 7 8 9 0]
Please let me know, what is the most efficient way of coding to implement this?
Thanks

Respuestas (2)

Matt J
Matt J el 24 de Nov. de 2023
Editada: Matt J el 24 de Nov. de 2023
A = [8 2 3 5 5 6 7 8 9];
B = [1 3 2 3];
F=nan(1,numel(A)+numel(B));
F(cumsum(B)+(1:numel(B)) )=0;
F(isnan(F))=A
F = 1×13
8 0 2 3 5 0 5 6 0 7 8 9 0

Image Analyst
Image Analyst el 24 de Nov. de 2023
This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.
To learn fundamental concepts, invest 2 hours of your time here:

Categorías

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

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by