From: Kara Charaziak on
"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!
From: us on
"Kara Charaziak" <pink03(a)o2.pl> wrote in message <i00cpl$hgc$1(a)fred.mathworks.com>...
> "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!

no...
you'd obviously have to adjust the matrices to match a common size...

us
From: Ashish Uthama on
help cat
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cat.html


>> a=rand(5,4,7);
>> b=rand(5,4,13);
>> c=cat(3,a,b);
>> whos
Name Size Bytes Class Attributes

a 5x4x7 1120 double
b 5x4x13 2080 double
c 5x4x20 3200 double
From: Wayne King on
"Kara Charaziak" <pink03(a)o2.pl> wrote in message <i00cpl$hgc$1(a)fred.mathworks.com>...
> "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!

Hi Kara, I don't have Matlab running at the moment, but I think in this case you may need to use permute()

So assume your function returns

X = randn(20,10,5);
Y = randn(20,10,6);

X1 = permute(X,[3 2 1]);
Y1 = permute(Y,[3 2 1]);
NewData = [X1 ; Y1];
% then when you're done
NewData = permute(NewData,[3 2 1]);

I think :) It's always tricky to post Matlab code when Matlab isn't running...

Wayne
From: James Tursa on
"Kara Charaziak" <pink03(a)o2.pl> wrote in message <i00cpl$hgc$1(a)fred.mathworks.com>...
> "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! :) )

Yes, it works, but as others have already pointed out this technique is in general a bad programming practice because you are growing the size of NewData inside a loop. So for each iteration of the loop you have to copy the entire dataset to a new memory block to do the concatenation. For your small matrix sizes this doesn't make much difference, but on a larger problem it could make a great deal of difference. A better programming technique is to use cell arrays (as has already been suggested) and then at the end of your loop concatenate them all at once.

> 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?

Yes, but you will need to use reshape. e.g.,

A = rand(20,10,5);
B = rand(20,10,6)+10;
C = reshape([A(:);B(:)],20,10,[]);

James Tursa