由单一的数字矢量替换整数序列 [英] Replace sequence of integers in vector by single number

查看:101
本文介绍了由单一的数字矢量替换整数序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A 整数是载体,其中我想通过一个precise更换次数数字序列。

Let A be vector of integers in which I want to replace a sequence of numbers by a precise number.

示例:

A = [ 8 7 1 2 3 4 5 1 2 3 4 5 6 7 ]

和我要替换的序列 1 2 3 9

其结果将是:

B = [ 8 7 9 4 5 9 4 5 6 7 ]

任何意见?

推荐答案

这可能是与 strfind bsxfun -

This could be one approach with strfind and bsxfun -

pattern = [1 2 3];
replace_num = 9;

B = A
start_idx = strfind(A,pattern)            %// Starting indices of pattern 
B(start_idx) = replace_num  %// Replace starting indices with replacement
B(bsxfun(@plus,start_idx(:),1:numel(pattern)-1))=[]    %// Find all group
               %// indices of the pattern except the starting indices and
              %// then delete them

这篇关于由单一的数字矢量替换整数序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆