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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:02:12+00:00 2026-06-14T21:02:12+00:00

I have got a matrix in a loop which is m= -132.5901 -137.2695 -114.1264

  • 0

I have got a matrix in a loop which is

m=


 -132.5901    -137.2695   -114.1264   -131.4986   -134.5733       Inf
       Inf       Inf       Inf        -135.2631   -121.7322       -119.7471
 -132.7978    -123.8068   -135.9385       Inf      -134.1602     -117.6997
 -130.1930    -134.0093   -137.4125   -128.7641       Inf        -116.0473

I want a command which can be used in loop to get the answer as:

 -132.5901 -137.2695 -114.1264 -131.4986 -134.5733  -119.7471  
 -132.7978 -123.8068 -135.9385 -135.2631 -121.7322  -117.6997
 -130.1930 -134.0093 -137.4125 -128.7641 -134.1602  -116.0473

I don’t want a single vector using isfinite command. The matrix size should be retained automatically using Matlab.

  • 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-14T21:02:13+00:00Added an answer on June 14, 2026 at 9:02 pm

    I’m going to assume that in retaining the size of the original matrix, you want us to pad the bottom of the solution matrix with NaN. I’m also going to assume that there might be more than one Inf in any given column. In that case the following loop-based solution works for me:

    %# Set up an example matrix
    M = [Inf 2 Inf 4; 5 Inf 6 7; Inf Inf 8 Inf];
    [T, N] = size(M);
    
    %# Get an index of finite elements
    I1 = isfinite(M);
    
    %# Solution 1
    Soln1 = NaN(T, N);
    for n = 1:N
        CurCol = M(:, n);
        Soln1(1:sum(I1(:, n)), n) = CurCol(I1(:, n));
    end
    

    There is probably a fully-vectorized solution to this problem for the general case (there definitely is – see my update below). However, I’d be surprised if it yields much of a speed-up over the loop-based solution. Single loops are now very fast in Matlab IF you set your problem up so you can operate on the columns of a matrix (as opposed to the rows), since these elements are allocated sequentially in memory.

    Now, let us assume that there is only one Inf per column. In this case, the problem is much easier to solve, and can be done with the following one-liner:

    %# Solution 2
    Soln2 = [reshape(M(isfinite(M)), T-1, N); NaN(1, N)];
    

    Solution 2 will obviously fail for the example matrix I set up, since 6 elements can not be re-arranged into a T-1 by N (ie 2 by 4) matrix.

    UPDATE: Okay, so for no good reason, it was annoying me that I couldn’t do it without a loop. Thus, here is a loop-less solution:

    %# Solution 3
    Soln3 = NaN(T, N);
    I2 = bsxfun(@plus, cumsum(isfinite(M)), (0:T:T*(N-1)));
    I2 = I2(:);
    I2(I1 == 0) = [];
    Soln3(I2) = M(isfinite(M));
    

    Quick (very) non-rigorous speed test on loop versus non-loop solution:

    Elapsed time is 0.203997 seconds.
    Elapsed time is 0.251969 seconds.
    

    The non-loop solution will probably improve (relatively speaking) though if M is bigger.

    Now I need to get back to work 🙂

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

Sidebar

Related Questions

I have got a matrix which gives me a 2-dimensional discrete distribution (N²->R) in
I have got a form into which information is entered, and a drop down
I have got a table [newsletter] in the db, which saves the email address,
I have got a few tables which I am trying to join. I just
I need help, I got a problem with this matrix, I need to have
I've got 2 different files, one of them is an input matrix (X) which
I have got a square matrix consisting of elements either 1 or 0. An
I have a 10GB CSV file which is essentially a huge square matrix. I
I have to create a Matrix class, and I got some problem to overload
I have calibrated stereo cameras and got the extrinsic matrix. I know the Translation

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.