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:
Storing into matrices

Subject: Storing into matrices

From: ali Abusnina

Date: 28 Sep, 2010 18:08:06

Message: 1 of 10

Hello,

I have a for loop that generates a row vector in each cycle. This for loop contains 8 cycles and the generated row vector is overritten in each of those cycles. i wonder how can I store those vectors before they are overriten so that by the end of the for loop I end up with a n by 8 matrix.

Subject: Storing into matrices

From: Sean

Date: 28 Sep, 2010 18:13:05

Message: 2 of 10

"ali Abusnina" <owise_83@hotmail.com> wrote in message <i7tau6$r9s$1@fred.mathworks.com>...
> Hello,
>
> I have a for loop that generates a row vector in each cycle. This for loop contains 8 cycles and the generated row vector is overritten in each of those cycles. i wonder how can I store those vectors before they are overriten so that by the end of the for loop I end up with a n by 8 matrix.

nx8_mat =zeros(n,8)
for ii = 1:n
    nx8_mat(ii,:) = calculate_your_row_vector();
end

Subject: Storing into matrices

From: ali Abusnina

Date: 28 Sep, 2010 18:56:20

Message: 3 of 10

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7tb7g$gp0$1@fred.mathworks.com>...
> "ali Abusnina" <owise_83@hotmail.com> wrote in message <i7tau6$r9s$1@fred.mathworks.com>...
> > Hello,
> >
> > I have a for loop that generates a row vector in each cycle. This for loop contains 8 cycles and the generated row vector is overritten in each of those cycles. i wonder how can I store those vectors before they are overriten so that by the end of the for loop I end up with a n by 8 matrix.
>
> nx8_mat =zeros(n,8)
> for ii = 1:n
> nx8_mat(ii,:) = calculate_your_row_vector();
> end

But this results in having the whole matrix filled i with the same figure.

Subject: Storing into matrices

From: Sean

Date: 28 Sep, 2010 19:17:19

Message: 4 of 10

"ali Abusnina" <owise_83@hotmail.com> wrote in message <i7tdok$4u0$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7tb7g$gp0$1@fred.mathworks.com>...
> > "ali Abusnina" <owise_83@hotmail.com> wrote in message <i7tau6$r9s$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I have a for loop that generates a row vector in each cycle. This for loop contains 8 cycles and the generated row vector is overritten in each of those cycles. i wonder how can I store those vectors before they are overriten so that by the end of the for loop I end up with a n by 8 matrix.
> >
> > nx8_mat =zeros(n,8)
> > for ii = 1:n
> > nx8_mat(ii,:) = calculate_your_row_vector();
> > end
>
> But this results in having the whole matrix filled i with the same figure.

No it'll be filled in with whatever you tell it to.
If that's the same thing on every iteration then yes it will.
Examples:
This will do that you're saying
A = rand(1,8)
n = 5
nx8_mat = zeros(n,8)
for ii = 1:8
   nx8_mat(ii,:) = A;
end

This won't:
n = 5
nx8_mat = zeros(n,8)
for ii = 1:8
   nx8_mat(ii,:) = rand(1,8);
end

Nor will this
n = 5
nx8_mat = zeros(n,8)
for ii = 1:8
   nx8_mat(ii,:) = ii*[1:8];
end

Subject: Storing into matrices

From: Cagdas Ozgenc

Date: 28 Sep, 2010 19:36:24

Message: 5 of 10

"ali Abusnina" <owise_83@hotmail.com> wrote in message <i7tau6$r9s$1@fred.mathworks.com>...
> Hello,
>
> I have a for loop that generates a row vector in each cycle. This for loop contains 8 cycles and the generated row vector is overritten in each of those cycles. i wonder how can I store those vectors before they are overriten so that by the end of the for loop I end up with a n by 8 matrix.

Usually the common syntax is as follows:

T = [];

for i=1:8
    L = generate whatever you like
    T = [T;L];
end

at the end T should contain all rows. If row vectors are generated result will be 8 by n not n by 8.

Subject: Storing into matrices

From: James Tursa

Date: 28 Sep, 2010 19:52:18

Message: 6 of 10

"Cagdas Ozgenc" <cagdasozgenc@hotmail.com> wrote in message <i7tg3o$b79$1@fred.mathworks.com>...
>
> Usually the common syntax is as follows:
>
> T = [];
>
> for i=1:8
> L = generate whatever you like
> T = [T;L];
> end
>
> at the end T should contain all rows. If row vectors are generated result will be 8 by n not n by 8.

That is usually the *bad* syntax often employed by newbie MATLAB users. Your listed syntax above is growing a matrix inside a loop, and at each iteration the entire data set must be copied. Inefficient and wasteful. Preallocation using the zeros function, as shown in an earlier post, is a much better method.

James Tursa

Subject: Storing into matrices

From: Sean

Date: 28 Sep, 2010 19:55:10

Message: 7 of 10


> Usually the common syntax is as follows:
>
> T = [];
>
> for i=1:8
> L = generate whatever you like
> T = [T;L];
> end
>
> at the end T should contain all rows. If row vectors are generated result will be 8 by n not n by 8.

That is terrible advice and is _not at all_ what anyone on here advocates. For speed and good programming practice you preallocate your our matrix (in your example T). That means setting T to be the final size, not empty. If it's empty it has to grow on each iteration and will take a significant amount of time.

Since the poster knows the number of columns and the number of rows; this can be preallocated very easily.

Subject: Storing into matrices

From: Walter Roberson

Date: 28 Sep, 2010 20:08:11

Message: 8 of 10

On 10-09-28 02:55 PM, Sean wrote:
>
>> Usually the common syntax is as follows:
>>
>> T = [];
>>
>> for i=1:8
>> L = generate whatever you like
>> T = [T;L];
>> end
>>
>> at the end T should contain all rows. If row vectors are generated
>> result will be 8 by n not n by 8.
>
> That is terrible advice and is _not at all_ what anyone on here
> advocates.

I've suggested pretty much that syntax myself in a posting less than a week ago.

When people are having trouble with basics, sometimes it doesn't make any
sense to detail the more complicated pre-allocation methods. More complicated
because sometimes one has to do a pre-pass or non-trivial calculation to
figure out how much space would be required, and if the amount varies per pass
then one has to get in to counter variables that get iterated as the loop
increases, and one has to get in to how to write at particular offsets into
arrays and so on. It's all too much to take in for someone stuck back at (for
example) realizing that one needs to use two distinct variables instead of
using one variable for two different purposes.

Remember a fundamental tenant of programming: first get it right and _then_
worry about getting it faster.

Subject: Storing into matrices

From: ali Abusnina

Date: 29 Sep, 2010 08:15:22

Message: 9 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <i7ti4h$2l6$1@canopus.cc.umanitoba.ca>...
> On 10-09-28 02:55 PM, Sean wrote:
> >
> >> Usually the common syntax is as follows:
> >>
> >> T = [];
> >>
> >> for i=1:8
> >> L = generate whatever you like
> >> T = [T;L];
> >> end
> >>
> >> at the end T should contain all rows. If row vectors are generated
> >> result will be 8 by n not n by 8.
> >
> > That is terrible advice and is _not at all_ what anyone on here
> > advocates.
>
> I've suggested pretty much that syntax myself in a posting less than a week ago.
>
> When people are having trouble with basics, sometimes it doesn't make any
> sense to detail the more complicated pre-allocation methods. More complicated
> because sometimes one has to do a pre-pass or non-trivial calculation to
> figure out how much space would be required, and if the amount varies per pass
> then one has to get in to counter variables that get iterated as the loop
> increases, and one has to get in to how to write at particular offsets into
> arrays and so on. It's all too much to take in for someone stuck back at (for
> example) realizing that one needs to use two distinct variables instead of
> using one variable for two different purposes.
>
> Remember a fundamental tenant of programming: first get it right and _then_
> worry about getting it faster.


It worked fine this time but as you said the matrix is 8 by n. can you please advice on how to make it n by 8?

Subject: Storing into matrices

From: ali Abusnina

Date: 29 Sep, 2010 08:29:05

Message: 10 of 10

"ali Abusnina" <owise_83@hotmail.com> wrote in message <i7usiq$r64$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <i7ti4h$2l6$1@canopus.cc.umanitoba.ca>...
> > On 10-09-28 02:55 PM, Sean wrote:
> > >
> > >> Usually the common syntax is as follows:
> > >>
> > >> T = [];
> > >>
> > >> for i=1:8
> > >> L = generate whatever you like
> > >> T = [T;L];
> > >> end
> > >>
> > >> at the end T should contain all rows. If row vectors are generated
> > >> result will be 8 by n not n by 8.
> > >
> > > That is terrible advice and is _not at all_ what anyone on here
> > > advocates.
> >
> > I've suggested pretty much that syntax myself in a posting less than a week ago.
> >
> > When people are having trouble with basics, sometimes it doesn't make any
> > sense to detail the more complicated pre-allocation methods. More complicated
> > because sometimes one has to do a pre-pass or non-trivial calculation to
> > figure out how much space would be required, and if the amount varies per pass
> > then one has to get in to counter variables that get iterated as the loop
> > increases, and one has to get in to how to write at particular offsets into
> > arrays and so on. It's all too much to take in for someone stuck back at (for
> > example) realizing that one needs to use two distinct variables instead of
> > using one variable for two different purposes.
> >
> > Remember a fundamental tenant of programming: first get it right and _then_
> > worry about getting it faster.
>
>
> It worked fine this time but as you said the matrix is 8 by n. can you please advice on how to make it n by 8?

oK, I managed to do it. Thanks for you help. but what is meant by prallocation? and how can it be done?

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