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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:01:25+00:00 2026-06-02T20:01:25+00:00

I am new to Octave/Matlab so so far I know, you can apply matrix

  • 0

I am new to Octave/Matlab so so far I know, you can apply matrix operation (like *) or cell-wise operation (like .*).

Now I have the problem which lies between those two modes.

For example (this is just EXAMPLE) I have a matrix (10,10) and a vector (10,1). I would like to work with this matrix in slices (in this case column slices) and add vector to them. So add vector to first column, add vector to second column, …., add vector to last column. And in the result acquire matrix (10,10) of course.

So far I come up with two approaches:

  1. manually loop over columns, and add the vector

  2. repeat the vector and then add entire repeated vector (so now it is matrix really) to the matrix

The second one uses vectorization approach, however consumes memory a lot, in the first case, there is no vectorization approach (manual loop), but the memory is not overused.

QUESTION — is there some nice third, way, slice mode? In which I could say, view matrix as slices, add vector to slices, and drop such view, and treat matrix as usual?

  • 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-02T20:01:26+00:00Added an answer on June 2, 2026 at 8:01 pm

    You can use Matlab’s bult in binary-singleton-expansion (bsxfun) to achieve the results you desire in a memory efficient manner.

    x = ones(10); %// 10x10 matrix
    y = 1:10; %// 10x1 matrix
    z = bsxfun(@plus, x, y)
    

    This will give the following ouput

    z =
    
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
     2     3     4     5     6     7     8     9    10    11
    

    Use of the repmat command is generally wasteful (as you pointed out in your question) and can usually be avoided. See this article for a detailed description of bsxfun versus repmat

    http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/

    For multiplication at least, you can achieve a result using a trick involving diagonal matrices. You can use the sparse keyword to reduce the memory usage for the interim storage of the diagonal matrix

    x = ones(10); %// 10x10 matrix
    y = 1:10; %// 10x1 matrix
    yd = sparse(diag(y)); %// 10x10 matrix, but memory is only used to store data and its indicies
    
    z =  yd * x %// 10x10 matrix
    

    However, the bsxfun solution is generally superior.

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

Sidebar

Related Questions

NEW EDIT: I have narrowed my problem to this - i have a view
New to StackOverflow here. I'm working on the first Euler problem and have run
New to Entity framework. I am using EF4 and I have implemented for now
I'm new to Octave/Matlab and I want to plot a 3D-Graph. I was able
New to iPhone app development, I have a problem compiling (or Build and Run)
New to PHP and MySQL, have heard amazing things about this website from Leo
new to c#. I'm trying to make a simple system where I can search
New to objective-c and I just can't find the answer to this question anywhere.
new to java and brand new to the site. I have a JLabel added
I currently started to work with octave for some data analysis and have some

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.