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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:20:42+00:00 2026-06-02T12:20:42+00:00

I have matrix p=[1 2 3 4; 5 6 7 8; 10 20 30

  • 0

I have matrix

p=[1 2 3 4; 
   5 6 7 8; 
  10 20 30 50];

I’d like to compute the sum of the elements of each row that are between column iMin and iMax, with iMin and iMax being different for each row

e.g for

iMin = [3 2 1];
iMax = [4 4 3];

the result is

[7 21 60]

Is there an easy Octave / Matlab way to do this without loops ?

  • 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-02T12:20:45+00:00Added an answer on June 2, 2026 at 12:20 pm

    You can achieve this using logical indexing, following a similar approach to my answer to your other question.

    Assuming that iMin and iMax have the same number of entries as the number of rows in p you can horizonally tile the column indices for p i.e. [1:size(p,2)] and compare this to vertical tilings of iMin and iMax to generate a logical index into p for the entries that satisfy your criterion so:

    c_min=repmat(iMin',1,size(p,2))
    c_max=repmat(iMax',1,size(p,2))
    c_ind=repmat([1:size(p,2)],size(p,1),1)
    result=sum(p.*(c_ind>=c_min & c_ind<=c_max),2)
    

    Giving:

    result =
    
       7
      21
      60
    

    No loops 🙂

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

Sidebar

Related Questions

I have a matrix in R that I would like to take a single
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),
I have a function compute() that has parallelized matrix multiplication inside of it using
I have a matrix declared like int **matrix , and I know that the
I have a matrix that looks like this: SNP G1 G2 G3 marker1 TT
I have a matrix mat and would like to calculate the mean of the
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have an undirected graph on matris by vertex adjacency relations like that; /*
Have a matrix report now that has Position, Hours and Wages for a location
I have a set of N objects, and I'd like to compute a NxN

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.