My question is as follows:
I have two arrays with the same length, one contains the start points, and the other contains the end points. I'd like to "decompress" the two array, to get the third array that contains all the numbers between the start points and the end points. Is there a more efficient way to do this than looping through all the start and end points?
e.g.
start = [2 1 4 7];
end = [5 4 6 8];
=> results = [ 2:5 1:4 4:6 7:8];
Note the order of the numbers have to be preserved in the results, so the numbers between the first start-end pair appear first, and those of the second group appear right after the first one, and so on.
Any tips will be greatly appreciated!
|
|
0
|
|
|
|
Reply
|
Kuei
|
1/21/2010 9:02:04 PM |
|
On Jan 21, 1:02=A0pm, "Kuei-Chun Hsu" <b92902...@ntu.edu.tw> wrote:
> My question is as follows:
>
> I have two arrays with the same length, one contains the start points, an=
d the other contains the end points. I'd like to "decompress" the two array=
, to get the third array that contains all the numbers between the start po=
ints and the end points. Is there a more efficient way to do this than loop=
ing through all the start and end points?
>
> e.g.
> start =3D [2 1 4 7];
> end =3D [5 4 6 8];
>
> =3D> results =3D [ 2:5 =A01:4 =A04:6 =A07:8];
>
> Note the order of the numbers have to be preserved in the results, so the=
numbers between the first start-end pair appear first, and those of the se=
cond group appear right after the first one, and so on.
>
> Any tips will be greatly appreciated!
Do you want your "results" variable to be a cell array, where each
element consists of those smaller consecutive arrays, or do you want a
matrix in which these values are one after another?
Either way:
start1 =3D [2 1 4 7];
end1 =3D [5 4 6 8]; %end is a matlab special word
resultC =3D arrayfun(@(x,y)(x:y),start1,end1,'uni',false);
resultM =3D cell2mat(arrayfun(@(x,y)(x:y),start1,end1,'uni',false));
%%%%%%%%%%%%
resultC =3D
[1x4 double] [1x4 double] [1x3 double] [1x2 double]
resultM =3D
2 3 4 5 1 2 3 4 4 5 6
7 8
-Nathan
|
|
0
|
|
|
|
Reply
|
Nathan
|
1/21/2010 9:08:16 PM
|
|
Thank you so much Natha! I need a row vector that contains all the numbers (resultM). I've never known Matlab have functions like those, that could avoid lots of loops in my program. (Though I heard there are performance issues, it's still very cool!!)
-Kuei-Chun
Nathan <ngreco32@gmail.com> wrote in message <bb5492fc-b9e3-452f-8d48-98fe3dae1011@a15g2000yqm.googlegroups.com>...
> On Jan 21, 1:02 pm, "Kuei-Chun Hsu" <b92902...@ntu.edu.tw> wrote:
> > My question is as follows:
> >
> > I have two arrays with the same length, one contains the start points, and the other contains the end points. I'd like to "decompress" the two array, to get the third array that contains all the numbers between the start points and the end points. Is there a more efficient way to do this than looping through all the start and end points?
> >
> > e.g.
> > start = [2 1 4 7];
> > end = [5 4 6 8];
> >
> > => results = [ 2:5 1:4 4:6 7:8];
> >
> > Note the order of the numbers have to be preserved in the results, so the numbers between the first start-end pair appear first, and those of the second group appear right after the first one, and so on.
> >
> > Any tips will be greatly appreciated!
>
> Do you want your "results" variable to be a cell array, where each
> element consists of those smaller consecutive arrays, or do you want a
> matrix in which these values are one after another?
>
> Either way:
> start1 = [2 1 4 7];
> end1 = [5 4 6 8]; %end is a matlab special word
> resultC = arrayfun(@(x,y)(x:y),start1,end1,'uni',false);
> resultM = cell2mat(arrayfun(@(x,y)(x:y),start1,end1,'uni',false));
> %%%%%%%%%%%%
> resultC =
> [1x4 double] [1x4 double] [1x3 double] [1x2 double]
> resultM =
> 2 3 4 5 1 2 3 4 4 5 6
> 7 8
>
> -Nathan
>
|
|
0
|
|
|
|
Reply
|
Kuei
|
1/21/2010 9:28:04 PM
|
|
"Kuei-Chun Hsu" <b92902123@ntu.edu.tw> wrote in message <hjafcb$jsm$1@fred.mathworks.com>...
> My question is as follows:
>
> I have two arrays with the same length, one contains the start points, and the other contains the end points. I'd like to "decompress" the two array, to get the third array that contains all the numbers between the start points and the end points. Is there a more efficient way to do this than looping through all the start and end points?
>
> e.g.
> start = [2 1 4 7];
> end = [5 4 6 8];
>
> => results = [ 2:5 1:4 4:6 7:8];
>
> Note the order of the numbers have to be preserved in the results, so the numbers between the first start-end pair appear first, and those of the second group appear right after the first one, and so on.
>
> Any tips will be greatly appreciated!
Although I recommend the arrayfun approach proposed by Nathan, here is an old-school-matlab one-liner:
startIDX = [2 1 4 7]
endIDX = [5 4 6 8] ;
[results,dummy] = find(cumsum(full(sparse(startIDX,1:numel(startIDX),1,max(endIDX)+1,numel(startIDX)) + sparse(endIDX+1,1:numel(startIDX),-1,max(endIDX)+1,numel(startIDX))))) ;
Jos
|
|
0
|
|
|
|
Reply
|
Jos
|
1/22/2010 9:42:05 AM
|
|
> "Kuei-Chun Hsu"
> > My question is as follows:
> >
> > I have two arrays with the same length, one contains the start points, and the other
> > contains the end points. I'd like to "decompress" the two array, to get the third array that
> > contains all the numbers between the start points and the end points. Is there a more
> > efficient way to do this than looping through all the start and end points?
A for loop should be faster. So if you mean something faster go for the loop.
(if you mean something evil go for the eval: http://www.mathworks.com/matlabcentral/newsreader/view_thread/270573#709902.
In this post other alternatives are discussed and Jos's as well. It all began there...)
eval(fliplr(')''gelO .live m''''I''(psid'))
|
|
0
|
|
|
|
Reply
|
Oleg
|
1/22/2010 9:52:05 AM
|
|
|
4 Replies
189 Views
(page loaded in 0.073 seconds)
Similiar Articles: generate random points in any shaped polygon? - comp.soft-sys ...Generating an array with a series of start and end points - comp ... generate random points in any shaped polygon? - comp.soft-sys ..... there knows of a way of generating ... Integrate Planck's law of radiation - comp.lang.fortranGenerating an array with a series of start and end points - comp ... Integrate Planck's law of radiation - comp.lang.fortran ..... and then compute the integrals between ... Generating a `random' number between 0 and 1 with a higher ...Generating an array with a series of start and end points - comp ... Generating a `random' number between 0 and 1 with a higher ... Love is like war, Easy to start, Hard ... generate/genvar, for loop and procdural (always/initial) block ...... clock[i] = 0; end end always begin for (j ... Your first example is a generate-for and, being handled at comp ... with some reference (bit, bit range, array ... Random array? - comp.cad.solidworksGenerating an array with a series of start and end points - comp ... C++ Random Numbers | DaniWeb put names in an array, generate random numbers from 0 to ... Numbers between a range - comp.lang.awkGenerating an array with a series of start and end points - comp ... Numbers between a range - comp.lang.awk... non-overlapping sequence of numbers Given a start and a end ... generate random values between min max values - comp.soft-sys ...Generating an array with a series of start and end points - comp ..... do you want a > matrix in which these values ... full(sparse(startIDX,1:numel(startIDX),1,max ... Loop to evaluate consecutive values in array - comp.soft-sys ...Generating an array with a series of start and end points - comp ... Is there a more efficient way to do this than loop ... where each element consists of those smaller ... JTable - How To Loop Through Looking For Selected Rows? - comp ...Generating an array with a series of start and end points - comp ... Is there a more efficient way to do this than loop= ing through ... I need a row vector that contains ... max and min of multiple cell arrays - comp.soft-sys.matlab ...Generating an array with a series of start and end points - comp ..... max(endIDX)+1,numel(startIDX)) + sparse(endIDX+1,1:numel(startIDX),-1,max ... random points ... C++ Random Numbers | DaniWebThis will give you a series of random cards from ... put names in an array, generate random numbers from 0 to ... push_back( buff ); } shuffle( names.begin(), names.end ... PHP: range - ManualEver wanted to generate an array with a range of column names for use in Excel file ... function createColumnsArray ($end_column, $first_letters = '') { $columns = array(); 7/24/2012 8:20:41 AM
|