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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:05:12+00:00 2026-05-28T05:05:12+00:00

I have a matrix like the following (arbitrary cols/rows): 1 0 0 0 0

  • 0

I have a matrix like the following (arbitrary cols/rows):

1    0    0    0    0
1    2    0    0    0
1    2    3    0    0
1    2    3    4    0
1    2    3    4    5
1    2    5    0    0
1    2    5    3    0
1    2    5    3    4
1    4    0    0    0
1    4    2    0    0
1    4    2    3    0
1    4    2    5    0
1    4    2    5    3
1    4    5    0    0
1    4    5    3    0
2    0    0    0    0
2    3    0    0    0
2    3    4    0    0
2    3    4    5    0
2    5    0    0    0
2    5    3    0    0
2    5    3    4    0
3    0    0    0    0
3    4    0    0    0
3    4    2    0    0
3    4    2    5    0
3    4    5    0    0

and now I want to get all rows where the first element is a certain value X and the last element (that is the last element != 0) is a certain value Y, OR turned around: the first is Y and the last is X.

Can’t see any speedful code which does NOT use a for-loop 🙁
Thanks!

EDIT: To filter all rows with a certain first element is really easy, you don’t need to help me here. So let’s assume I only want to do the following: Filter all rows where the last element (i.e. the last element != 0 in each row) is either X or Y.

EDIT
Thanks a lot for your posts. I benchmarked the three possible solutions with a matrix of 473408*10 elements. Here’s the benchmarkscript:
http://pastebin.com/9hEAWw9a

The results were:

t1 = 2.9425 Jonas
t2 = 0.0999 Brendan
t3 = 0.0951 Oli

So thanks a lot you guys, I’m sticking with Oli’s solution and thus accept it. Thanks though for all the other solutions!

  • 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-28T05:05:13+00:00Added an answer on May 28, 2026 at 5:05 am

    Here is a trick: Look for numbers with a 0 on the right, and sum them all:

    H=[1 2 0 0 0;
       2 3 1 0 0;
       4 5 8 0 0;
       8 5 4 2 2];
    
    lastNumber=sum(H.*[H(:,2:end)==0 true(size(H,1),1)],2)
    
    ans =
    
         2
         1
         8
         2
    

    The rest is easy:

    firstNumber=H(:,1);
    
    find( (firstNumber==f) & (lastNumber==l) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to achieve the following: Have a AxBxC matrix (where A,B,C are integers).
I have a matrix mat and would like to calculate the mean of the
I have a matrix in R that I would like to take a single
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have a vector, v3, that looks like this: v3 <- matrix(c(1,1,3,3,2,2, 2,3,1,2,3,1, 3,2,2,1,1,3),
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix in MATLAB and I need to find the 99% value
I have a matrix A consisting of 200 vectors of size d. I want
I have the first matrix which should account for each users (in lines) which
I have a big matrix and I want to retrieve a set of values

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.