How to Resize data in column vector?

2 views (last 30 days)
Sagar Dhage
Sagar Dhage on 31 Jul 2014
Answered: Marco Castelli on 31 Jul 2014
I have column vector A=(0;0;0;0;0;232;222;245;342;232; 0;0;0;0;0;0;0345;323;324;345;343;0;0;0) How I can keep only one zero such that I get A=(0;232;222;245;342;232;0;0345;323;324;345;343;0;)?

Answers (3)

Azzi Abdelmalek
Azzi Abdelmalek on 31 Jul 2014
A=[0;0;0;0;0;232;222;245;342;232; 0;0;0;0;0;0;0345;323;324;345;343;0;0;0]
idx=[0 A'==0 0]
ii=strfind(idx,[0 1])
jj=strfind(idx,[1 0])-1
ind=cell2mat(arrayfun(@(x,y) x:y-1,ii,jj,'un',0))
A(ind)=[]

Azzi Abdelmalek
Azzi Abdelmalek on 31 Jul 2014
Edited: Azzi Abdelmalek on 31 Jul 2014
A=[0;0;0;0;0;232;222;245;342;232; 0;0;0;0;0;0;0345;323;324;345;343;0;0;0]
idx=[1 diff(A') ]==0 & A'==0
A(idx)=[]

Marco Castelli
Marco Castelli on 31 Jul 2014
A=[0;0;0;0;0;232;222;245;342;232; 0;0;0;0;0;0;0345;323;324;345;343;0;0;0];
idx = [];
for i1 = 2:length(A)
if and(A(i1)==0,A(i1-1)==0)
idx = [idx, i1];
end
end
A(idx) = [];

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!