Puzzler for a Monday
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Given a cell array of strings
A = {'MATLAB','HURRAY','SPARKLY','KITTENS','FUN'};
and a particular string value
B = 'KITTENS';
ensure that B is the last element of the cell array. If it isn't, move it to the end of A.
You cannot assume that B appears at all (in which case return A unchanged), but you can assume B does not appear more than once.
[I have a solution, but I post this as a puzzle because I bet someone can find a slicker one.]
0 comentarios
Respuestas (6)
Sean de Wolski
el 22 de Jul. de 2013
idx = ismember(A,B);
C = cat(isrow(A)+1,A(~idx),A(idx))
1 comentario
Evan
el 22 de Jul. de 2013
Awesome. I especially like the "isrow" trick. I'll be remembering that one.
Abdullah Caliskan
el 27 de En. de 2015
k=ismember(A,B) A(k)='' if sum(k)==0 A=A; else A=[A B] end
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings 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!