Vectorized method to filling in array based on logical array
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Noahyt
el 4 de Ag. de 2016
Comentada: Noahyt
el 4 de Ag. de 2016
I am trying to fill in a new array using values that I have stored in an input vector (sort of like reformat, but more general).
I have a logical array that has true values at locations that correspond to where I want to place one of my input vector values. ONce I have used a value, I want to discard it and use the next value in the vector for the next "open" spot.
I realize that might not have been totally clear, so for example:
I have the input vector and input array
in_vec = [1,2,3,4,5];
in_arr = [0,0,0,0,0; 0,1,1,1,0; 0,0,1,1,0]
and I would like to get the output
out_arr = [0,0,0,0,0; 0,1,2,3,0; 0,0,4,5,0]
I know that I can do this using a for loop that iterates over every item in the logical array and inputting items in the target array, I'm wondering whether there is a smarter way to do this using a vectorized (or other) method?
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 4 de Ag. de 2016
Editada: Azzi Abdelmalek
el 4 de Ag. de 2016
Edit
in_vec = [1,2,3,4,5]
in_arr = [0,0,0,0,0; 0,1,1,1,0; 0,0,1,1,0]
out_arr =zeros(size(in_arr'))
out_arr(logical(in_arr'))=in_vec
out_arr=out_arr'
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!