How to substitute one value in vector by vector?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
KSSV
2021-9-23
编辑:KSSV
2021-9-23
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)
更多回答(4 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Denoising and Compression 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!