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

  • SEARCH
  • Home
  • 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 165853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:59:42+00:00 2026-05-11T11:59:42+00:00

I want to delete several specific values from a matrix (if they exist). It

  • 0

I want to delete several specific values from a matrix (if they exist). It is highly probable that there are multiple copies of the values in the matrix.

For example, consider an N-by-2 matrix intersections. If the pairs of values [a b] and [c d] exist as rows in that matrix, I want to delete them.

Let’s say I want to delete rows like [-2.0 0.5] and [7 7] in the following matrix:

intersections =     -4.0000    0.5000    -2.0000    0.5000     2.0000    3.0000     4.0000    0.5000    -2.0000    0.5000 

So that after deletion I get:

intersections =      -4.0000    0.5000     2.0000    3.0000     4.0000    0.5000 

What’s the most efficient/elegant way to do this?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T11:59:43+00:00Added an answer on May 11, 2026 at 11:59 am

    Try this one-liner (where A is your intersection matrix and B is the value to remove):

    A = [-4.0 0.5;      -2.0 0.5;       2.0 3.0;       4.0 0.5;      -2.0 0.5]; B = [-2.0 0.5]; A = A(~all(A == repmat(B,size(A,1),1),2),:); 

    Then just repeat the last line for each new B you want to remove.

    EDIT:

    …and here’s another option:

    A = A((A(:,1) ~= B(1)) | (A(:,2) ~= B(2)),:); 

    WARNING: The answers here are best used for cases where small floating point errors are not expected (i.e. with integer values). As noted in this follow-up question, using the ‘==’ and ‘~=’ operators can cause unwanted results. In such cases, the above options should be modified to use relational operators instead of equality operators. For example, the second option I added would be changed to:

    tolerance = 0.001;   % Or whatever limit you want to set A = A((abs(A(:,1)-B(1)) > tolerance) | (abs(A(:,2)-B(2)) > tolerance),:); 

    Just a quick head’s up! =)


    SOME RUDIMENTARY TIMING:

    In case anyone was really interested in efficiency, I just did some simple timing for three different ways to get the subindex for the matrix (the two options I’ve listed above and Fanfan’s STRMATCH option):

    >> % Timing for option #1 indexing: >> tic; for i=1:10000, index = ~all(A == repmat(B,size(A,1),1),2); end; toc; Elapsed time is 0.262648 seconds. >> % Timing for option #2 indexing: >> tic; for i=1:10000, index = (A(:,1) ~= B(1)) | (A(:,2) ~= B(2)); end; toc; Elapsed time is 0.100858 seconds. >> % Timing for STRMATCH indexing: >> tic; for i=1:10000, index = strmatch(B,A); end; toc; Elapsed time is 0.192306 seconds. 

    As you can see, the STRMATCH option is faster than my first suggestion, but my second suggestion is the fastest of all three. Note however that my options and Fanfan’s do slightly different things: my options return logical indices of the rows to keep, and Fanfan’s returns linear indices of the rows to remove. That’s why the STRMATCH option uses the form:

    A(index,:) = []; 

    while mine use the form:

    A = A(index,:); 

    However, my indices can be negated to use the first form (indexing rows to remove):

    A(all(A == repmat(B,size(A,1),1),2),:) = [];    % For option #1 A((A(:,1) == B(1)) & (A(:,2) == B(2)),:) = [];  % For option #2 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I got this question answered on the Surface community site:… May 12, 2026 at 12:13 am
  • Editorial Team
    Editorial Team added an answer A Vector is basically just a growable array, so you… May 12, 2026 at 12:13 am
  • Editorial Team
    Editorial Team added an answer You could simply do: if (Dns.GetHostAddresses(hostName).Length == 0) { //… May 12, 2026 at 12:13 am

Related Questions

I've got a weird thing going here in Powershell 1.0 and I don't understand
Background: I have a module which declares a number of instance methods module UsefulThings
Git has a much-touted(?) octopus-merge capability that can merge many heads into one. But
The problem is during inserting data from staging table during import routine. The system

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.