How to substitute one value in vector by vector?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Premysl Stastny
el 23 de Sept. de 2021
Respondida: Matt J
el 23 de Sept. de 2021
Hello,
I have got a vectro x=(-1 1 -1 1) and I need to replace positions containing -1 for a vector (1 2 3) and positions containing 1 for a vector (3 2 1). so the result should be x=(1 2 3 3 2 1 1 2 3 3 2 1).
Could somebody help me?
Thanks a lot
0 comentarios
Respuesta aceptada
KSSV
el 23 de Sept. de 2021
Editada: KSSV
el 23 de Sept. de 2021
x=[-1 1 -1 1] ;
iwant = cell(1,length(x)) ;
for i = 1:length(x)
if x(i) == -1
iwant{i} = [1 2 3 ];
elseif x(i) == 1
iwant{i} = [3 2 1] ;
end
end
iwant = cell2mat(iwant)
%% No loop
% No loop
x=[-1 1 -1 1] ;
iwant = cell(1,length(x)) ;
iwant(x==1) = {[3 2 1]} ;
iwant(x==-1) = {[1 2 3]} ;
iwant = cell2mat(iwant)
Más respuestas (4)
Matt J
el 23 de Sept. de 2021
x=[-1 1 -1 1];
i=(x+3)/2;
V=[1 2 3;3 2 1].';
X=reshape( V(:,i) ,1,[])
0 comentarios
Matt J
el 23 de Sept. de 2021
x=[-1 1 -1 1];
x=strrep(10*x,10,[3,2,1]);
x=strrep(x,-10,[1,2,3])
0 comentarios
Ver también
Categorías
Más información sobre Denoising and Compression 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!