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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:33:11+00:00 2026-06-05T16:33:11+00:00

I have two matrices of different sizes. Let’s just define matrix {a} as a(1:10)

  • 0

I have two matrices of different sizes. Let’s just define matrix {a} as a(1:10) <10 x 1> and matrix {b} as b(6:10) <5 x 1>. I need a for loop or equivalent (bsxfun) which gets the difference between matrix {a} and {b}, the code will iterate based on the size of matrix {b}. For example, the first value of matrix {a} is 1, the code will get the difference of all of matrix {b} values. So, it will run a total of 5 times. The next value of matrix {a} is 2, the code will iterate 5 times. The code will iterate until the end of matrix {a} which is value 10.

If you can could you write both a for loop without bsxfun and one with and explain how you indexed the values. Also, just for my edification, instead of two matrices, how would the code change if there were N matrices (N>2)?

Thank you.

  • 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-05T16:33:13+00:00Added an answer on June 5, 2026 at 4:33 pm

    Here’s a loop solution and a repmat solution.

    % Define some example data.

    Edit: a and b are column vectors, not row vectors.

    a = [ 1:10 ]';
    

    b = [ 6:10 ]'; % 5:10 has vertical size of 6, not 5, so to match the question 6:10 is used.

    First, the very basic loop solution: loop through all aIndex,bIndex pairs, subtract the difference of elements addressed by aIndex and bIndex and store the result in LoopDifferenceMatrix(aIndex, bIndex).

    for aIndex = 1:size(a,1)
        for bIndex = 1:size(b,1)
            LoopDifferenceMatrix(aIndex, bIndex) = a(aIndex) - b(bIndex);
        end
    end
    

    This is an alternative repmat solution. Replicate a horizontally by using repmat so that its horizontal size matches size(b,1) (the horizontal size of b). Then replicate transposed b vertically by using repmat so that its vertical size matches size(a,1) (the original horizontal size of a). Subtract replicated a from replicated b and store the result in DifferenceMatrix.

    DifferenceMatrix = repmat(a, 1, size(b,1)) - repmat(b', size(a,1), 1);
    
    DifferenceMatrix =
    -5    -6    -7    -8    -9
    -4    -5    -6    -7    -8
    -3    -4    -5    -6    -7
    -2    -3    -4    -5    -6
    -1    -2    -3    -4    -5
     0    -1    -2    -3    -4
     1     0    -1    -2    -3
     2     1     0    -1    -2
     3     2     1     0    -1
     4     3     2     1     0 
    
    isequal(DifferenceMatrix, LoopDifferenceMatrix)
    ans =
    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say I have two matrices, of a different size: a = zeros(1,100); b
I have two matrices A and B , both of which are Nx3 matrices.
In short: I have two matrices (or arrays): import numpy block_1 = numpy.matrix([[ 0,
Hi I need to plot a boxplot in R. I have two matrices a
Here's an interesting question :) I have two vectors of matrices which I want
I want to pass two matrices as argument. These matrices have different size and
I have two matrices x and y, both are results from different algorithms/routines that
I have two 4x4 OPENGL Matrices - 1st matrix holds rotation and position of
I have two matrices like the following ones: '01/01/2010' 1 '02/01/2010' 2 '03/01/2010' 3
I have two matrices. The first contains values, and the second contains names corresponding

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.