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 7028763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:27:03+00:00 2026-05-28T00:27:03+00:00

Given: square matrix, and list which represents the index of rows to be removed,

  • 0

Given: square matrix, and list which represents the index of rows to be removed, and it also represent at the same time the index of the columns to be removed (it is square matrix, so only one list is needed).

output: the square matrix, with BOTH the rows and the columns columns in the list removed.

Assume valid list of indices.

This is an example
enter image description here

So the above says to remove the second and the 4th rows, and also the second and the 4th column.

I could not figure how to use Delete[] to remove both rows and columns at the same time, and I really did not want to make a list of each individual element index I want to remove.

But I could use Delete to remove rows.

This below how I solved it, I removed the rows first, then transposed the matrix, and then removed the rows of the new matrix (which will be the columns of the original), then transposed the result back to obtain what I wanted.

like this:

a = {{0, 5, 2, 3, 1, 0}, {4, 3, 2, 5, 1, 3}, {4, 1, 3, 5, 3, 2}, {4, 
    4, 1, 1, 1, 5}, {3, 4, 4, 5, 3, 3}, {5, 1, 4, 5, 2, 0}};
del = {{2}, {4}};
a = Delete[a, del];
a = Delete[Transpose[a], del];
(a = Transpose[a]) // MatrixForm

my question: Is there a shorter way using Delete (or another one of those expert tricks) to do this in a better way ?

thanks

  • 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-05-28T00:27:03+00:00Added an answer on May 28, 2026 at 12:27 am

    In cases where you want to remove the same indexed columns and rows I would use Part. For example to see a with columns and rows 2 and 4 removed:

    a[[{1, 3, 5, 6}, {1, 3, 5, 6}]] // MatrixForm
    

    To make it more general you could create something in which you combine DeleteCases with Range and the list of column/row indexes but in the absence of further information I haven’t tried to do that (yet).

    Edit

    remove[a_?MatrixQ, pos_List] := Module[{tmp, length = Length[a]},
    
      tmp = DeleteCases[Range[length], Alternatives @@ pos];
    
      a[[tmp, tmp]]
    
      ]
    
    remove[a,{2,4}]
    {{0, 2, 1, 0}, {4, 3, 3, 2}, {3, 4, 3, 3}, {5, 4, 2, 0}}
    

    Edit2

    remove2[a_?MatrixQ, pos_List] := Module[{tmp, length = Length[a]},
    
      tmp = Complement[Range[length], pos];
    
      a[[tmp, tmp]]
    
      ]
    
    remove2[a,{2,4}]
    {{0, 2, 1, 0}, {4, 3, 3, 2}, {3, 4, 3, 3}, {5, 4, 2, 0}}
    

    test both of these for your real world problem.

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

Sidebar

Related Questions

Given a matrix A (not neccessarily square) with independent columns, I was able to
Given a binary matrix in which every row and column contains exactly only one
I have some simple code that represents a graph using a square boolean matrix
Given a square matrix, where each cell is black or white. Design an algorithm
I have a 10GB CSV file which is essentially a huge square matrix. I
Given an NxN binary matrix (containing only 0's or 1's), how can we go
How can I create a polygon(only a square in my case) around a given
When given a square matrix, what would be the best way to find all
Given a grid where I know the number of rows (which is fixed), and
Given a square (described by x, y, width, height) and an angle (in radians)

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.