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:
How to select adjacent elements from an array?

Subject: How to select adjacent elements from an array?

From: Bart

Date: 23 Sep, 2013 08:43:07

Message: 1 of 4

Hello,

So if for example I have an array which looks like this:

array = [1 2 3 4 5 8 9 10 11 73 74]

I want a script which creates separate vectors with the adjacent elements, so:

data.first = [1 2 3 4 5]
data.second = [8 9 10 11]
data.third = [73 74]

The number of vectors is equal to the number of groups of adjacent elements.

Thank you.

Subject: How to select adjacent elements from an array?

From: someone

Date: 23 Sep, 2013 14:45:06

Message: 2 of 4

"Bart" wrote in message <l1ouur$o15$1@newscl01ah.mathworks.com>...
> Hello,
>
> So if for example I have an array which looks like this:
>
> array = [1 2 3 4 5 8 9 10 11 73 74]
>
> I want a script which creates separate vectors with the adjacent elements, so:
>
> data.first = [1 2 3 4 5]
> data.second = [8 9 10 11]
> data.third = [73 74]
>
> The number of vectors is equal to the number of groups of adjacent elements.
>
> Thank you.

% I don't have MATLAB installed on this computer, so this is untested.
% But it should get you started.
% I think using cell arrays makes more sense than structures.
% You can convert yourself, if you really want structures.

array = [1 2 3 4 5 8 9 10 11 73 74];

temp = abs(diff(array));
indx = find(temp > 1);

indx = [1 indx length(array)]

for ii = 1:(length(indx)-1)
   data{ii} = array(indx(ii):indx(ii+1));
end

% There may be bugs if all the numbers are consectutive,
% for example. I'll let you work those out.

Subject: How to select adjacent elements from an array?

From: Bart

Date: 24 Sep, 2013 06:11:05

Message: 3 of 4

"someone" wrote in message <l1pk5i$kv7$1@newscl01ah.mathworks.com>...
> "Bart" wrote in message <l1ouur$o15$1@newscl01ah.mathworks.com>...
> > Hello,
> >
> > So if for example I have an array which looks like this:
> >
> > array = [1 2 3 4 5 8 9 10 11 73 74]
> >
> > I want a script which creates separate vectors with the adjacent elements, so:
> >
> > data.first = [1 2 3 4 5]
> > data.second = [8 9 10 11]
> > data.third = [73 74]
> >
> > The number of vectors is equal to the number of groups of adjacent elements.
> >
> > Thank you.
>
> % I don't have MATLAB installed on this computer, so this is untested.
> % But it should get you started.
> % I think using cell arrays makes more sense than structures.
> % You can convert yourself, if you really want structures.
>
> array = [1 2 3 4 5 8 9 10 11 73 74];
>
> temp = abs(diff(array));
> indx = find(temp > 1);
>
> indx = [1 indx length(array)]
>
> for ii = 1:(length(indx)-1)
> data{ii} = array(indx(ii):indx(ii+1));
> end
>
> % There may be bugs if all the numbers are consectutive,
> % for example. I'll let you work those out.

Thank you very much for your help. When I used your script it returned:
[1 2 3 4 5], [5 8 9 10 11] and [11 73 74].

I modified it to:

array = [1 2 3 4 5 8 9 10 11 73 74];

temp = abs(diff(array));
indx = find(temp > 1);

indx = [1 indx length(array)];

for ii = 1:(length(indx)-1)
    if ii == 1;
        data{ii} = array(indx(ii):indx(ii+1));
    else
   data{ii} = array(indx(ii)+1:indx(ii+1));
    end
end

And now it works perfectly. Thank you.

Subject: How to select adjacent elements from an array?

From: someone

Date: 24 Sep, 2013 15:34:08

Message: 4 of 4

"Bart" wrote in message <l1radp$ctp$1@newscl01ah.mathworks.com>...
> "someone" wrote in message <l1pk5i$kv7$1@newscl01ah.mathworks.com>...
> > "Bart" wrote in message <l1ouur$o15$1@newscl01ah.mathworks.com>...
> > > Hello,
> > >
> > > So if for example I have an array which looks like this:
> > >
> > > array = [1 2 3 4 5 8 9 10 11 73 74]
> > >
> > > I want a script which creates separate vectors with the adjacent elements, so:
> > >
> > > data.first = [1 2 3 4 5]
> > > data.second = [8 9 10 11]
> > > data.third = [73 74]
> > >
> > > The number of vectors is equal to the number of groups of adjacent elements.
> > >
> > > Thank you.
> >
> > % I don't have MATLAB installed on this computer, so this is untested.
> > % But it should get you started.
> > % I think using cell arrays makes more sense than structures.
> > % You can convert yourself, if you really want structures.
> >
> > array = [1 2 3 4 5 8 9 10 11 73 74];
> >
> > temp = abs(diff(array));
> > indx = find(temp > 1);
> >
> > indx = [1 indx length(array)]
> >
> > for ii = 1:(length(indx)-1)
> > data{ii} = array(indx(ii):indx(ii+1));
> > end
> >
> > % There may be bugs if all the numbers are consectutive,
> > % for example. I'll let you work those out.
>
> Thank you very much for your help. When I used your script it returned:
> [1 2 3 4 5], [5 8 9 10 11] and [11 73 74].
>
> I modified it to:
>
> array = [1 2 3 4 5 8 9 10 11 73 74];
>
> temp = abs(diff(array));
> indx = find(temp > 1);
>
> indx = [1 indx length(array)];
>
> for ii = 1:(length(indx)-1)
> if ii == 1;
> data{ii} = array(indx(ii):indx(ii+1));
> else
> data{ii} = array(indx(ii)+1:indx(ii+1));
> end
> end
>
> And now it works perfectly. Thank you.

% Glad to help. Good catch.
% A slightly simplier modification would be:

array = [1 2 3 4 5 8 9 10 11 73 74];

temp = abs(diff(array));
indx = find(temp > 1);

indx = [0 indx length(array)];

for ii = 1:(length(indx)-1)
   data{ii} = array((indx(ii)+1):indx(ii+1));
end

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