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 shorten this code? (multiple initializations)

Subject: How to shorten this code? (multiple initializations)

From: Mongkut Piantanakulchai

Date: 12 Nov, 2010 09:05:05

Message: 1 of 10

How can I shorten this code? (multiple initialization)

REL=zeros(396432,1);
SEX=zeros(396432,1);
AGE=zeros(396432,1);
MAR=zeros(396432,1);
ATT=zeros(396432,1);
ED=zeros(396432,1);
WK=zeros(396432,1);
RG=zeros(396432,1);
STATUS=zeros(396432,1);

Thanks,

Subject: How to shorten this code? (multiple initializations)

From: Jan Simon

Date: 12 Nov, 2010 09:48:04

Message: 2 of 10

Dear Mongkut,

The shown code looks very compact already. I cannot imaging an advantage except for using a variable instead of a fixed size:

  n = 396432
  REL = zeros(n, 1);
  SEX = zeros(n, 1);
  AGE = zeros(n, 1);
  MAR = zeros(n, 1);
  ATT = zeros(n, 1);
  ED = zeros(n, 1);
  WK = zeros(n, 1);
  RG = zeros(n, 1);
  STATUS = zeros(n, 1);

I definitely would *not* try to create a loop and create the variables dynamically using EVAL.
Creating a temporary variable "tmp=zeros(n, 1)" and assigning it to each variable is at first faster: "REL = tmp; SEX = tmp;" etc. But if you ever modify the values (I assume you do) this would cause a deep data copy and consume more time than your original version.

Kind regards, Jan

Subject: How to shorten this code? (multiple initializations)

From: Aurelien Queffurust

Date: 12 Nov, 2010 12:33:04

Message: 3 of 10

I definetely understand your question. It reminds me this Fex utility: PUTVAR by John D'Errico which was a POTW selected by Jiro Doke :
http://blogs.mathworks.com/pick/2010/05/07/putting-variables-back-in-the-base-workspace/

It happens that I would like to have the same utility when preallocating variables.

Subject: How to shorten this code? (multiple initializations)

From: Walter Roberson

Date: 12 Nov, 2010 12:52:16

Message: 4 of 10

On 12/11/10 3:05 AM, Mongkut Piantanakulchai wrote:
> How can I shorten this code? (multiple initialization)
>
> REL=zeros(396432,1);
> SEX=zeros(396432,1);
> AGE=zeros(396432,1);
> MAR=zeros(396432,1);
> ATT=zeros(396432,1);
> ED=zeros(396432,1);
> WK=zeros(396432,1);
> RG=zeros(396432,1);
> STATUS=zeros(396432,1);

N = 396432;
[REL,SEX,AGE,MAR,ATT,ED,WK,RG,STATUS] = deal(zeros(N,1));

Subject: How to shorten this code? (multiple initializations)

From: Bruno Luong

Date: 12 Nov, 2010 13:00:04

Message: 5 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <4MaDo.9689$8m.5868@newsfe09.iad>...

>
> N = 396432;
> [REL,SEX,AGE,MAR,ATT,ED,WK,RG,STATUS] = deal(zeros(N,1));

This code is not really equivalent to OP's since the data are shared (as Jan's right-on remark).

Bruno

Subject: How to shorten this code? (multiple initializations)

From: Aurelien Queffurust

Date: 12 Nov, 2010 13:16:05

Message: 6 of 10


> N = 396432;
> [REL,SEX,AGE,MAR,ATT,ED,WK,RG,STATUS] = deal(zeros(N,1));

Thanks a lot , It is a big deal!!
WOW I never thought using deal for this case !!!

Just a remark about the deal function:
the HELP of this function is great since it uses an example with the DIR command :

The example given when typing :
>>help deal
 
is :
******************
direc = dir; filenames = {};
        [filenames{1:length(direc),1}] = deal(direc.name);
******************

 And we know that DIR is often used when we write a MATLAB code. So the DEAL command becomes easily familiar for any MATLAB programmers.
But if "help deal" is useful for a MATLAB beginner , I don't think it is the case when we type:
>> doc deal

I am not sure that a MATLAB beginner which reads the 3 given examples will understand the power of DEAL!

...agian it is just my point of view!

Aurélien Queffurust
http://blog.developpez.com/matlab4geek/

Subject: How to shorten this code? (multiple initializations)

From: Jan Simon

Date: 12 Nov, 2010 13:31:05

Message: 7 of 10

Dear Aurelien,

> direc = dir; filenames = {};
> [filenames{1:length(direc),1}] = deal(direc.name);

What about this:
  direc = dir;
  filenames = {direc.name};

Looks leaner, doesn't it?

Jan

Subject: How to shorten this code? (multiple initializations)

From: Aurelien Queffurust

Date: 12 Nov, 2010 13:41:03

Message: 8 of 10

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <ibjfip$acd$1@fred.mathworks.com>...
> Dear Aurelien,
>
> > direc = dir; filenames = {};
> > [filenames{1:length(direc),1}] = deal(direc.name);
>
> What about this:
> direc = dir;
> filenames = {direc.name};
>
> Looks leaner, doesn't it?
>
> Jan

Yeap definetly agree with you . Setting filenames to an empty cell is not useful if we think about performance ... but I was just quoting the help of deal function

Subject: How to shorten this code? (multiple initializations)

From: Aurelien Queffurust

Date: 12 Nov, 2010 13:54:03

Message: 9 of 10

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <ibjfip$acd$1@fred.mathworks.com>...
> Dear Aurelien,
>
> > direc = dir; filenames = {};
> > [filenames{1:length(direc),1}] = deal(direc.name);
>
> What about this:
> direc = dir;
> filenames = {direc.name};
>
> Looks leaner, doesn't it?
>
> Jan

OOps sorry I have answered too fast, I didn't catch that you suggested another way to get the same list ( if we add a transpose {direc.name}' )

Subject: How to shorten this code? (multiple initializations)

From: Jan Simon

Date: 12 Nov, 2010 14:05:04

Message: 10 of 10

Dear Walter,

The DEAL approach is without doubt "shorter".
If the variables are used later, e.g. by setting the first element of each variable to 1, the DEAL method is 20% slower than the simple direct approach using 9 calls of ZEROS (Matlab 2009a, N as in the OP's example).

@Aurelien: I understood, that this is the help written by TMW. But this example is neither efficient nor nice.

Kind regards, Jan

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