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:
How to create many vctors or arrays quickly

Subject: How to create many vctors or arrays quickly

From: Shiguo

Date: 24 Jan, 2009 22:20:04

Message: 1 of 6

I want to create n vectors, each has the size of s.
I know I can do it like this:

a1 = zeros(s, 1);
a2 = a1;
...
an = a1;

But I want to create these vectors at the same time.
I used the following way:

[a1, a2, ..., an] = zeros(s, 5);

It says,
??? Error using ==> zeros
Too many output arguments.

Because I have a lot of tables to handle, for each table, I need to create n vectors of size s. The n and s are different for these tables. So I want to create vectors automatically at the same time for each table.

Is there any method to create vectors quickly? Thanks.

Subject: How to create many vctors or arrays quickly

From: Jos

Date: 24 Jan, 2009 22:32:02

Message: 2 of 6

"Shiguo " <jiangshiguo@gmail.com> wrote in message <glg46j$cdg$1@fred.mathworks.com>...
> I want to create n vectors, each has the size of s.
> I know I can do it like this:
>
> a1 = zeros(s, 1);
> a2 = a1;
> ...
> an = a1;
>
> But I want to create these vectors at the same time.
> I used the following way:
>
> [a1, a2, ..., an] = zeros(s, 5);
>
> It says,
> ??? Error using ==> zeros
> Too many output arguments.
>
> Because I have a lot of tables to handle, for each table, I need to create n vectors of size s. The n and s are different for these tables. So I want to create vectors automatically at the same time for each table.
>
> Is there any method to create vectors quickly? Thanks.

First of all, try to avoid doing this. Use
A = zeros(s,n)
and refer to a specific vector K as A(:,K), as in
B = A(:,3) + 2 ; % whatever

or use cell arrays or structs.

If you really think you must clutter your workspace with similar variables, take a look at DEAL. However, again, try to avoid this typical poor programming practice ...

hth
Jos

Subject: How to create many vctors or arrays quickly

From: Shiguo

Date: 24 Jan, 2009 22:33:01

Message: 3 of 6

The [[a1, a2, ..., an] = zeros(s, n)], that's I want to create a s*n matrix, so that each column of the matrix will be an vector. (Unfortunately, it seems it does not work in matlab.)

"Shiguo " <jiangshiguo@gmail.com> wrote in message <glg46j$cdg$1@fred.mathworks.com>...
> I want to create n vectors, each has the size of s.
> I know I can do it like this:
>
> a1 = zeros(s, 1);
> a2 = a1;
> ...
> an = a1;
>
> But I want to create these vectors at the same time.
> I used the following way:
>
> [a1, a2, ..., an] = zeros(s, 5);
>
> It says,
> ??? Error using ==> zeros
> Too many output arguments.
>
> Because I have a lot of tables to handle, for each table, I need to create n vectors of size s. The n and s are different for these tables. So I want to create vectors automatically at the same time for each table.
>
> Is there any method to create vectors quickly? Thanks.

Subject: How to create many vctors or arrays quickly

From: Shiguo

Date: 24 Jan, 2009 22:42:01

Message: 4 of 6

"Jos " <#10584@fileexchange.com> wrote in message <glg4t2$qdu$1@fred.mathworks.com>...
> First of all, try to avoid doing this. Use
> A = zeros(s,n)
> and refer to a specific vector K as A(:,K), as in
> B = A(:,3) + 2 ; % whatever
>
> or use cell arrays or structs.
>
> If you really think you must clutter your workspace with similar variables, take a look at DEAL. However, again, try to avoid this typical poor programming practice ...
>
> hth
> Jos
>
>

I see... Thanks.

Shiguo

Subject: How to create many vctors or arrays quickly

From: Pekka Kumpulainen

Date: 26 Jan, 2009 11:07:21

Message: 5 of 6

"Shiguo " <jiangshiguo@gmail.com> wrote in message <glg4ut$jg$1@fred.mathworks.com>...
> The [[a1, a2, ..., an] = zeros(s, n)], that's I want to create a s*n matrix, so that each column of the matrix will be an vector. (Unfortunately, it seems it does not work in matlab.)

A = zeros(s,n);
This is the s*n matrix you say you want. Each column of a matrix is a vector. The i'th column is A(:,i). A(:,3) will give you the third column vector etc.

Subject: How to create many vctors or arrays quickly

From: Shiguo

Date: 26 Jan, 2009 17:59:02

Message: 6 of 6

"Pekka Kumpulainen" <pekka.nospam.kumpulainen@tut.please.fi> wrote in message <glk5h9$gac$1@fred.mathworks.com>...
> "Shiguo " <jiangshiguo@gmail.com> wrote in message <glg4ut$jg$1@fred.mathworks.com>...
> > The [[a1, a2, ..., an] = zeros(s, n)], that's I want to create a s*n matrix, so that each column of the matrix will be an vector. (Unfortunately, it seems it does not work in matlab.)
>
> A = zeros(s,n);
> This is the s*n matrix you say you want. Each column of a matrix is a vector. The i'th column is A(:,i). A(:,3) will give you the third column vector etc.
>

Great! I know what to do now. Thanks!

Tags for 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