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:
Is it Ok to shrink with [ ]?

Subject: Is it Ok to shrink with [ ]?

From: Akim

Date: 13 Aug, 2010 11:54:04

Message: 1 of 9

Dear all,

I know that growing with indexing,

clear x
for i=1:10
    x(i)=1;
end,

is not recommended.

My question: does shrinking with indexing,

x=ones(1,10);
for i=1:10
    x(1)=[];
end,

cause memory problems? I.e. does Matlab reallocate memory for variable x every time x(1)=[]; is executed?

Thanks.

Subject: Is it Ok to shrink with [ ]?

From: Andy

Date: 13 Aug, 2010 12:23:04

Message: 2 of 9

It would appear so:

n = 10000;
x=rand(1,n); % data
y=x; % copies
z=x;

tic
for ix=n:-1:500
    x(ix) = []; % deleting from the end
end
toc

tic
for ix=1:n-499
    y(1) = []; % deleting from the beginning
end
toc

tic
z(500:end) = []; % deleting all at once
toc

% displays:

Elapsed time is 0.517248 seconds.
Elapsed time is 0.516376 seconds.
Elapsed time is 0.001210 seconds.

Subject: Is it Ok to shrink with [ ]?

From: Sean

Date: 13 Aug, 2010 13:24:08

Message: 3 of 9

"Andy " <myfakeemailaddress@gmail.com> wrote in message <i43df8$ba6$1@fred.mathworks.com>...
> It would appear so:
>
> n = 10000;
> x=rand(1,n); % data
> y=x; % copies
> z=x;
>
> tic
> for ix=n:-1:500
> x(ix) = []; % deleting from the end
> end
> toc
>
> tic
> for ix=1:n-499
> y(1) = []; % deleting from the beginning
> end
> toc
>
> tic
> z(500:end) = []; % deleting all at once
> toc
>
> % displays:
>
> Elapsed time is 0.517248 seconds.
> Elapsed time is 0.516376 seconds.
> Elapsed time is 0.001210 seconds.

You also have to be careful with a for loop since the total length may not be as long at the end (after removing spots) when it was when you initialized the loop.

Subject: Is it Ok to shrink with [ ]?

From: Andy

Date: 13 Aug, 2010 13:40:06

Message: 4 of 9

> You also have to be careful with a for loop since the total length may not be as long at the end (after removing spots) when it was when you initialized the loop.

I don't follow you. The total length at the end will NECESSARILY be less than the length when you initialized the loop, since you're using the loop to remove elements from the array. Are you saying to be careful not to do the following?:

n = 1000;
x = rand(1,n);
for ix = 1:800
    x(ix) = []; % will seem to work for a while, then will break
end

Subject: Is it Ok to shrink with [ ]?

From: Sean

Date: 13 Aug, 2010 13:56:05

Message: 5 of 9

"Andy " <myfakeemailaddress@gmail.com> wrote in message <i43hvm$rc0$1@fred.mathworks.com>...
> > You also have to be careful with a for loop since the total length may not be as long at the end (after removing spots) when it was when you initialized the loop.
>
> I don't follow you. The total length at the end will NECESSARILY be less than the length when you initialized the loop, since you're using the loop to remove elements from the array. Are you saying to be careful not to do the following?:
>
> n = 1000;
> x = rand(1,n);
> for ix = 1:800
> x(ix) = []; % will seem to work for a while, then will break
> end

I meant more like this:
x = rand(1,1000);
for ix = 1:length(x)
    if(mod(100,ix)==0)
        x(ix) = []; %This will cause an error at some point
    end
end

You would want a while loop
ix = 1;
while ix < length(x)
    %would be stable
    if(mod(100,ix)==0)
        x(ix) = []; %This will cause an error at some point
    else
        ix = ix+1;
    end
end

Subject: Is it Ok to shrink with [ ]?

From: Sean

Date: 13 Aug, 2010 14:06:04

Message: 6 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i43itl$r5n$1@fred.mathworks.com>...

> You would want a while loop
> ix = 1;
> while ix < length(x)
> %would be stable
> if(mod(100,ix)==0)
> x(ix) = []; %This will cause an error at some point
> else
> ix = ix+1;
> end
> end

That was a bad example since it's using an unchanging ix for its criterion to delete. Thus it would delete the whole thing.
I meant something like this:
if(mod(100,x(ix))==0)

Subject: Is it Ok to shrink with [ ]?

From: James Tursa

Date: 13 Aug, 2010 15:05:22

Message: 7 of 9

"Akim " <aaa@bbb.ccc> wrote in message <i43bor$n7h$1@fred.mathworks.com>...
> Dear all,
>
> I know that growing with indexing,
>
> clear x
> for i=1:10
> x(i)=1;
> end,
>
> is not recommended.
>
> My question: does shrinking with indexing,
>
> x=ones(1,10);
> for i=1:10
> x(1)=[];
> end,
>
> cause memory problems? I.e. does Matlab reallocate memory for variable x every time x(1)=[]; is executed?

Yes. Shrinking an array with [] inside a loop is exactly the same problem as growing an array inside a loop. In both cases MATLAB will be forced to copy the array at each iteration. There *is* a way to delete the *last* column of an array without a data copy involved using a mex routine, but that is likely beyond the scope of this thread.


James Tursa

Subject: Is it Ok to shrink with [ ]?

From: Andy

Date: 13 Aug, 2010 15:22:21

Message: 8 of 9

Sean, I see your point. Since the while loop recalculates length(x) on each loop iteration, you won't accidentally index outside the bounds of x. I would argue, however, that both of these loops are quite dangerous in that they present a silent error: it seems the intent is to eliminate every 100th element of x. But of course this is not what either loop does.

It should be pointed out that shrinking an array in a loop did not give a warning in m-lint:

for ix = 1:100
    x(ix) = rand; % warning
end

for ix = 100:-1:1
    x(ix) = []; % no warning
end

Subject: Is it Ok to shrink with [ ]?

From: Akim

Date: 17 Aug, 2010 06:12:04

Message: 9 of 9

Thanks all for your help.

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