Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8530767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:19:35+00:00 2026-06-11T09:19:35+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:19:37+00:00Added an answer on June 11, 2026 at 9:19 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any real difference between these two methods? Method 1: bool IsNumOverFive(int num)
1)Is there any difference between these two keywords for the elements of collections??( Copy
Is there any practical difference between these two extension methods? class Extensions { public
Is there any difference between these two methods of moving a file? System.IO.FileInfo f
Is there any difference between these two LINQ statements: var query = from a
When I clone a repository, is there any difference between these two URLs? Without
I would like to know is there any difference in performance between these two
Is there any difference between the following two examples and should one be preferred
Is there any performance difference between these two approaches? // First approach, iterating until
Is there any difference between these two types of declarations performance-wise? local object =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.