From: Kara Charaziak on 24 Jun 2010 14:50 Hey, I have a function that generates a data matrix e.g. 7x8 I use this function in a loop and I want to have all output data in one big matrix. it looks like that for i=1:10 [data]=function_name end so when for i=1 i get one large data matrix e.g. 7x8 and for next i value I gen another data matrix e.g. 12x8 (the matrices generated by the function have always the same amount of columns but may have different numbers of rows) so then I have one large matrix with 19 rows (7+12) and so on for next i value. any ideas?
From: us on 24 Jun 2010 15:05 "Kara Charaziak" <pink03(a)o2.pl> wrote in message <i009d1$519$1(a)fred.mathworks.com>... > Hey, > I have a function that generates a data matrix e.g. 7x8 > I use this function in a loop and I want to have all output data in one big matrix. > > it looks like that > for i=1:10 > [data]=function_name > end > > so when for i=1 i get one large data matrix e.g. 7x8 and for next i value I gen another data matrix e.g. 12x8 (the matrices generated by the function have always the same amount of columns but may have different numbers of rows) so then I have one large matrix with 19 rows (7+12) and so on for next i value. > any ideas? one of the many solutions fun=@(x) 1:x; % <- a simple test function... n=5; data=cell(n,1); for i=1:n data{i,:}=fun(i); end data %{ % data = [ 1] [1x2 double] [1x3 double] [1x4 double] [1x5 double] %} us
From: Wayne King on 24 Jun 2010 15:06 "Kara Charaziak" <pink03(a)o2.pl> wrote in message <i009d1$519$1(a)fred.mathworks.com>... > Hey, > I have a function that generates a data matrix e.g. 7x8 > I use this function in a loop and I want to have all output data in one big matrix. > > it looks like that > for i=1:10 > [data]=function_name > end > > so when for i=1 i get one large data matrix e.g. 7x8 and for next i value I gen another data matrix e.g. 12x8 (the matrices generated by the function have always the same amount of columns but may have different numbers of rows) so then I have one large matrix with 19 rows (7+12) and so on for next i value. > any ideas? Hi Kara, if the matrices have the same number of columns, you can concatenate them like this NewData = []; for i = 1:10 [data] = function_name NewData = [NewData ; data]; end as a simple example: NewData = []; for i = 1:10 data = randn(i,3); NewData = [NewData ; data]; end % NewData is 55x3 Is this what you had in mind? Wayne
From: Ashish Uthama on 24 Jun 2010 15:12 On Thu, 24 Jun 2010 14:50:09 -0400, Kara Charaziak <pink03(a)o2.pl> wrote: > Hey, > I have a function that generates a data matrix e.g. 7x8 > I use this function in a loop and I want to have all output data in one > big matrix. > > it looks like that > for i=1:10 > [data]=function_name > end > > so when for i=1 i get one large data matrix e.g. 7x8 and for next i > value I gen another data matrix e.g. 12x8 (the matrices generated by the > function have always the same amount of columns but may have different > numbers of rows) so then I have one large matrix with 19 rows (7+12) and > so on for next i value. > any ideas? Is there some way to know what the maximum number of rows would be? (either per function call, or the total). What is the general range of the number of rows in your real code? A not-good way to do this is via concatenation: data=rand(7,8); newData=rand(12,8) data = [data;newData] this is not good since the size of data increases at run-time. This is bad for performance, and more so if the matrix sizes are large. If you have some idea about the the expected number of rows, you can allocate the space (with extra buffer rows) earlier (say using ZEROS) and then truncate it depending on how much you actually got.
From: Kara Charaziak on 24 Jun 2010 15:46 "Wayne King" <wmkingty(a)gmail.com> wrote in message <i00aas$6p9$1(a)fred.mathworks.com>... > "Kara Charaziak" <pink03(a)o2.pl> wrote in message <i009d1$519$1(a)fred.mathworks.com>... > > Hey, > > I have a function that generates a data matrix e.g. 7x8 > > I use this function in a loop and I want to have all output data in one big matrix. > > > > it looks like that > > for i=1:10 > > [data]=function_name > > end > > > > so when for i=1 i get one large data matrix e.g. 7x8 and for next i value I gen another data matrix e.g. 12x8 (the matrices generated by the function have always the same amount of columns but may have different numbers of rows) so then I have one large matrix with 19 rows (7+12) and so on for next i value. > > any ideas? > > Hi Kara, if the matrices have the same number of columns, you can concatenate them like this > > NewData = []; > for i = 1:10 > [data] = function_name > NewData = [NewData ; data]; > end > > as a simple example: > > NewData = []; > for i = 1:10 > data = randn(i,3); > NewData = [NewData ; data]; > end > > % NewData is 55x3 > > Is this what you had in mind? > > Wayne This is exactly what I needed! works great. (it is so simple! :) ) now, when I deal with that there is another issue.. my function generates also the data in matrix n-by-m-by-t, e.g. 20-10-6 (the m and n are fixed only the third number changes for different data output) so when I get in loop first matrix 20-10-5 then next 20-10-6 I want to get a merged matrix 20-10-11 (5+6) Can I do it in similar way? thanks!
|
Next
|
Last
Pages: 1 2 3 4 Prev: Delaunay/interpolation from triangular mesh Next: Help in some matrix operations |