Indexing of cell array and a vector
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sergio Rojas Blanco
el 2 de Jun. de 2024
Comentada: Sergio Rojas Blanco
el 3 de Jun. de 2024
Hi guys, my problem has been raised before but with some differences.
I have a cell array of indexes, index. Its cells can be zero or vectors with several values. On the other hand I have a data vector, data. I need a cell array, out, that relates data to indexes, preferably without using a loop.
For example:
index = {[0], [0], [5], [0], [6, 7], [0], [0], [1], [0], [0], [0], [ 3]} ;
data = [100, 10, 20, 15, 11, 25, 200, 45];
Desired result:
out = {[0], [0], [11], [0], [25, 200], [0], [0], [100], [0], [0], [20]}
Can somebody help me?
4 comentarios
Respuesta aceptada
Star Strider
el 2 de Jun. de 2024
One approach —
index = {[0], [0], [5], [0], [6, 7], [0], [0], [1], [0], [0], [0], [ 3]} ;
data = [100, 10, 20, 15, 11, 25, 200, 45];
q = cellfun(@(x) data(x(x>0)), index, 'Unif',0);
Lvc = cellfun(@(x) all(x==0), index, 'Unif',0); % Using 'all' Is Necessary To Make The Logical Indices Work Correctly
out = index;
out(~[Lvc{:}]) = q(~[Lvc{:}])
.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing 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!