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!