"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.
