How to substitute one value in vector by vector?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Premysl Stastny
le 23 Sep 2021
Réponse apportée : Matt J
le 23 Sep 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 commentaires
Réponse acceptée
KSSV
le 23 Sep 2021
Modifié(e) : KSSV
le 23 Sep 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)
Plus de réponses (4)
Matt J
le 23 Sep 2021
x=[-1 1 -1 1];
i=(x+3)/2;
V=[1 2 3;3 2 1].';
X=reshape( V(:,i) ,1,[])
0 commentaires
Matt J
le 23 Sep 2021
x=[-1 1 -1 1];
x=strrep(10*x,10,[3,2,1]);
x=strrep(x,-10,[1,2,3])
0 commentaires
Voir également
Catégories
En savoir plus sur Denoising and Compression dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!