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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:21:40+00:00 2026-05-20T12:21:40+00:00

I welcome any help for the following code optimization problem: I have a collection

  • 0

I welcome any help for the following code optimization problem:

I have a collection of N sparse matrices of identical sizes ([s1 s2]) stored in a cell array A and a corresponding number of scalar weights stored in an vector w. I want to compute the sum of all the matrices in A weighted by the values stored in w. Through the iterations of my program, only the values in wchange. I can therefore compute a priori the number of non-zero elements in my result and pre-allocate some memory for it using spalloc.
For the moment I have something like:

result = spalloc(s1,s1,number_of_non_zero);
for i=1:N
    result = result + w(i)*A{i};
end

I really need do optimize this part which for the moment takes most of the computing time in my program (checked with the profiling tools).
Some additional information:
-The above code runs millions of times so even minor improvements are welcome.
-The matrices in A come from a finite element code (1D or 2D)
-I have no problem moving away from the cell structure if I can save some time (like using cell2mat(A))

Thank you for any hint on how to speed up this part of the code.

A.

  • 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-20T12:21:41+00:00Added an answer on May 20, 2026 at 12:21 pm

    I can’t say for sure if this solution will be more computationally-efficient, but it’s something else to try…

    If you really have to represent your matrices as sparse (i.e. full matrices take up too much memory) and the built-in sparse representation in MATLAB isn’t giving you the performance you desire, then you can try representing the sparse matrices in a different way. Specifically, you can represent them as N-by-3 matrices where the first two columns contain the row and column indices into the matrix for all the non-zero values and the third column contains the non-zero values. You can convert your data to this form using the function FIND like so:

    for iMatrix = 1:numel(A)
      [r,c,v] = find(A{iMatrix});
      A{iMatrix} = [r c v];
    end
    

    Each time you need to compute the weighted sum of these matrices, you first need to multiply the values by the weights:

    B = A;  %# Store a temporary copy of A
    for iMatrix = 1:numel(B)
      B{iMatrix}(:,3) = w(iMatrix).*B{iMatrix}(:,3);
    end
    

    Then, you can compute the final sum using the function ACCUMARRAY:

    B = vertcat(B{:});  %# Convert B from a cell array to an N-by-3 matrix
    result = accumarray(B(:,1:2),B(:,3));
    

    The variable result in this case will be a full matrix. If you need result to be a sparse matrix, you can add extra arguments in the call to ACCUMARRAY like so:

    result = accumarray(B(:,1:2),B(:,3),[],[],[],true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML page (say welcome.html) which contains an iframe to a page
Ok, i have simple scenario: have two pages: login and welcome pages. im using
I have some string resources, e.g. a user welcome string. By default it should
Welcome! I very enjoyed programming artificial intelligence in my studies - neural networks, expert
My homepage (or welcome page) will consist of data from two models (lets call
in web.xml i set my welcome file to a jsp within web.xml <welcome-file>WEB-INF/index.jsp</welcome-file> inside
XP-specific answers preferred, but others are welcome too.
I need a WiX 3 script to display to display only 2 dialogs: Welcome
How do I avoid read locks in my database? Answers for multiple databases welcome!
Like for example : To Create a string object with string Welcome to objective-C

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.