series of consecutive numbers in a matrix

5 views (last 30 days)
noa goldman
noa goldman on 10 Dec 2018
Commented: Anusha Sridharan on 20 Dec 2018
hi everyone,
i need to make a vec who has the num of the rows of the series of consecutive numbers.
how can i write a program to find where those consecutive numbers are? i've tried using a for loop but haven't really got anywhere
for eg
if this is my matrix :
numMat= [2 3 5; 6 1 3; 4 5 6; 3 3 3; 9 8 7; 1 2 3];
the vec will be :
indVec= [3,6]
my scrips is:
numMat= [2 3 5; 6 1 3; 4 5 6; 3 3 3; 9 8 7; 1 2 3];
indVec=[];
count=1;
for i= 1: size(numMat,1)
for j= 1: size (numMat,2)
if numMat(i,j)== numMat(i,j+1)
indVec (count)= i;
count= count+1;
end
end
end
  2 Comments
Stephen23
Stephen23 on 10 Dec 2018
@noa goldman: it is not appreciated when you delete your question like that. Now the answers have no context and are meaningless to anyone else. You unilaterally decided that our volunteers efforts should only be useful to you, which is very unfair for us.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 10 Dec 2018
Edited: Stephen23 on 10 Dec 2018
This is MATLAB, so get rid of the pointless loops:
>> M = [2 3 5; 6 1 3; 4 5 6; 3 3 3; 9 8 7; 1 2 3]
M =
2 3 5
6 1 3
4 5 6
3 3 3
9 8 7
1 2 3
>> X = find(all(diff(M,1,2)==1,2))
X =
3
6

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!