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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:58:13+00:00 2026-05-25T18:58:13+00:00

This is another simple ‘matrix’ question in Mathematica. I want to show how I

  • 0

This is another simple ‘matrix’ question in Mathematica. I want to show how I did this, and ask if there is a better answer.

I want to select all ‘rows’ from matrix based on value in the first column (or any column, I used first column here just as an example).

Say, find all rows where the entry in the first position is <=4 in this example:

    list = {{1, 2, 3},
           {4, 5, 8},
           {7 , 8, 9}}

So, the result should be

            {{1,2,3},
             {4,5,8}}

Well, the problem is I need to use Position, since the result returned by Position can be used directly by Extract. (but can’t be used by Part or [[ ]], so that is why I am just looking at Position[] ).

But I do not know how to tell Position to please restrict the ‘search’ pattern to only the ‘first’ column so I can do this in one line.

When I type

pos = Position[list, _?(# <= 4 &)]

it returns position of ALL entries which are <=4.

{{1, 1}, {1, 2}, {1, 3}, {2, 1}}

If I first get the first column, then apply Position on it, it works ofcourse

  list = {{1, 2, 3},
          {4, 5, 8},
          {7 , 8, 9}};
   pos = Position[list[[All, 1]], _?(# <= 4 &)]
   Extract[list, pos]
   -->       {{1, 2, 3}, {4, 5, 8}}

Also I tried this:

pos = Position[list, _?(# <= 4 &)];
pos = Select[pos, #[[2]] == 1 &]  (*only look at ones in the 'first' column*)

{{1, 1}, {2, 1}}---> 

and this gives me the correct positions in the first column. To use that to find all rows, I did

pos = pos[[All, 1]] (* to get list of row positions*)
---> {1, 2}

list[[ pos[[1]] ;; pos[[-1]], All]]
{{1, 2, 3}, 
 {4, 5, 8}}

So, to summarize, putting it all together, this is what I did:

method 1

 list = {{1, 2, 3},
         {4, 5, 8},
         {7 , 8, 9}};
 pos = Position[list[[All, 1]], _?(# <= 4 &)]
 Extract[list, pos]
  -->       {{1, 2, 3}, {4, 5, 8}}

method 2

list = {{1, 2, 3},
      {4, 5, 8},
      {7 , 8, 9}}

pos = Position[list, _?(# <= 4 &)];
pos = Select[pos, #[[2]] == 1 &];
pos = pos[[All, 1]];
list[[ pos[[1]] ;; pos[[-1]], All]]

{{1, 2, 3}, 
{4, 5, 8}}

 The above clearly is not too good. 

Is method 1 above the ‘correct’ functional way to do this?

For reference, this is how I do the above in Matlab:

EDU>> A=[1 2 3;4 5 8;7 8 9]
A =
     1     2     3
     4     5     8
     7     8     9

EDU>> A( A(:,1)<=4 , :)

     1     2     3
     4     5     8

I am trying to improve my ‘functional’ handling of working with matrices in Mathematica commands, this is an area I feel I am not good at working with lists. I find working with matrices easier for me.

The question is: Is there is a shorter/more functional way to do this in Mathematica?

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-25T18:58:14+00:00Added an answer on May 25, 2026 at 6:58 pm

    If you want the rows that meet the criteria, use Cases:

    Cases[list, {n_, __} /; n <= 4]
    
    (* {{1, 2, 3}, {4, 5, 8}} *)
    

    If you want the positions within the list rather than the rows themselves, use Position instead of Cases (restricted to the first level only):

    Position[list, {n_, __} /; n <= 4, {1}]
    
    (* {{1}, {2}} *)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simple one. I want to replace a sub-string with another sub-string
Another simple question : is there any way to tell flex to prefer a
Seems like this should be simple, but powershell is winning another battle with me.
I have a simple script which is used to start another program. This other
Another jquery calculation question. I've this, which is sample code from the plugin site
(I asked this question in another way , and got some interesting responses but
I had this answer on another post I asked: I believe the VS designer
I saw this tip in another question and was wondering if someone could explain
I'm going to give this another try because my last question might have been
Is this another MS Access 2000(2007)-ism? If I type the following: foo = blah

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.