Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
keep rows in matrix which difference is 1

Subject: keep rows in matrix which difference is 1

From: Edwin

Date: 20 Mar, 2013 17:07:11

Message: 1 of 2

Hi,
I was wondering if someone could help me
find the way to solve this:
I want to keep the rows in a matrix which difference is 1 and
move the last value of 2nd column for which this condition is true, to the first row,
parallel I also want to keep those values that are unique in the matrix.
for example if I have
A=
[2 3
3 4
4 5
5 6
7 8
9 10
10 11
12 13
13 14
14 15
16 17]

I should get
A=
[2 6
7 8
9 11
12 15
16 17]

I have tried a nested for loop,
however it does not work.

Thanks!

Subject: keep rows in matrix which difference is 1

From: Bruno Luong

Date: 20 Mar, 2013 19:19:06

Message: 2 of 2

Use http://www.mathworks.fr/matlabcentral/fileexchange/24255-consecutive-vector-spliter

A=[2 3
3 4
4 5
5 6
7 8
9 10
10 11
12 13
13 14
14 15
16 17]

[f l] = SplitVec(A(:,1),'cons','first','last'); % FEX
[A(f,1) A(l,2)]

ans =

     2 6
     7 8
     9 11
    12 15
    16 17

% Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us