Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

matlab - Deleting matrix elements by = [] vs reassigning matrix

Is there any difference between these two methods for deleting elements in Matlab:

ElementsToDelete = [0 0 1 0 1 0 0 1 1 0]

A = 1:10
A(ElementsToDelete) = []

%Versus

A = 1:10
A = A(~ElementsToDelete)

Are there times when one method is more appropriate than the other? Is there a difference in efficiency? Or are they completely interchangeable?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

A = rand(1e3, 1);
b = A<0.5;

tic; 
for ii = 1:1e5
    a = A;       
    a(b) = [];
end
toc

tic; 
for ii = 1:1e5
    a = A;        
    a = a(~b);
end
toc

Results:

Elapsed time is 1.654146 seconds
Elapsed time is 1.126325 seconds

So the difference is a speed factor of 1.5 in favour of re-assigning. This however, is worse:

A = rand(1e4, 1);

stop = 0;    
for jj = 1:10
    a = A;
    start = tic;
    for ii = 1:1e5
        a(a < rand) = [];
    end
    stop = stop + toc(start);
end
avg1 = stop/10


stop = 0;    
for jj = 1:10
    a = A;
    start = tic;
    for ii = 1:1e5
        a = a(a > rand);
    end
    stop = stop + toc(start);
end
avg2 = stop/10

avg1/avg2

Results:

avg1 = 1.1740235 seconds
avg2 = 0.1850463 seconds

avg1/avg2 = 6.344485136963019

So, the factor's increased to well over 6.

My guess is that deletion (i.e., assigning with []) re-writes the entire array on each and every occurrence of a true in the internal loop through the logical indices. This is hopelessly inefficient, as becomes apparent when testing it like this. Re-assigning on the other hand can determine the size of the new array beforehand and initialize it accordingly; no re-writes needed.

Why the JIT does not compile the one into the other is a mystery to me, because deletion is a far more intuitive notation IMHO. But, as you see, it is inefficient compared to alternatives, and should thus be used sparingly. Never use it inside loops!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...