Can this be vectorized?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Patrick Mboma
el 30 de Oct. de 2014
Comentada: Patrick Mboma
el 30 de Oct. de 2014
Dear all,
I have two vectors A and B of respective sizes na and nb, with na>=nb. All elements in B are unique while the elements in A are not. But all elements in A are in B. I would like to create a mapping between A and B and one way to do that is as follows:
A=nan(na,1);
for ii=1:nb
A(A==B(ii))=ii;
end
This works well be becomes prohibitively expensive for large na. And so my question is whether there is a way of getting rid of the for loop and/or creating this mapping differently.
Thanks,
P.
1 comentario
Respuesta aceptada
Andrei Bobrov
el 30 de Oct. de 2014
Editada: Andrei Bobrov
el 30 de Oct. de 2014
[lc,ii] = ismember(A,B);
A(lc) = ii(lc);
or with nan
out = ii;
out(~lc) = nan;
or jast (with zeros)
out = ii;
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical 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!