在MATLAB中的向量中添加围绕一个值的其他值的其他值 [英] Adding additional ones that surround other values of one in a vector in MATLAB

查看:93
本文介绍了在MATLAB中的向量中添加围绕一个值的其他值的其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于MATLAB中的零和一向量,其中零表示时间上的事件,我想在现有零之前和之后添加其他零,以捕获其他变化.

Given a vector of zeros and ones in MATLAB, where the zeros represent an event in time, I would like to add additional ones before and after the existing ones in order to capture additional variation.

示例:我想将[0;0;1;0;0]转换为[0;1*;1;1*;0],其中1*是新添加的.

Example: I would like to turn [0;0;1;0;0] into [0;1*;1;1*;0] where 1* are newly added ones.

推荐答案

假定A为输入列向量-

%// Find all neighbouring indices with a window of [-1 1] 
%// around the positions/indices of the existing ones
neigh_idx = bsxfun(@plus,find(A),[-1 1])

%// Select the valid indices and set them in A to be ones as well
A(neigh_idx(neigh_idx>=1 & neigh_idx<=numel(A))) = 1


或将Image Processing Toolbox中的 imdilate 与矢量一起使用长度3-


Or use imdilate from Image Processing Toolbox with a vector kernel of ones of length 3 -

A = imdilate(A,[1;1;1])

这篇关于在MATLAB中的向量中添加围绕一个值的其他值的其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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