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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:46:24+00:00 2026-05-26T13:46:24+00:00

I am not very comfortable with using accumarray function in Matlab, though I have

  • 0

I am not very comfortable with using accumarray function in Matlab, though I have begun to appreciate its powers! I was wondering if I could input 2 cols in the VAL field of accumarray function. Please see –

sz = 3  ; % num_rows for each ID
mat1 = [1 20 ; 1 40 ; 1 50 ; 2 10 ; 2 100 ; 2 110] ; % Col1 is ID, Col2 is Value
idx  = [30 1000 ; 30 1200 ; 30 1500 ; 30 1000 ; 30 1200 ; 30 1500 ] ; 
% col1: index ID, col2: value

mat1 is ID returns while idx is index returns. For simplicity, idx returns are repeated to match mat1. All IDs in mat1 have same rows. Even idx has the same rows.

[~,~,n] = unique(mat1(:,1), 'rows', 'last') ;
fncovariance = @(x,y) (x.*y)/sz ;
accumarray(n, [x(:,2) y(:,2)], [], fncovariance) % --> FAILS as VAL is not-vector!

You can see that I’m trying to calculate covariance (cov(x,y,1)) but cannot use Matlab’s function directly as mat1 has IDs and I need covariance for each ID w.r.t Index.

Ansmat:

    1 2444.4
    2 7888.9
  • 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-26T13:46:25+00:00Added an answer on May 26, 2026 at 1:46 pm

    The short answer is no. In the accumarray() help, the key part is:

    “VAL must be a numeric, logical, or character vector with the same length
    as the number of rows in SUBS. VAL may also be a scalar whose value is
    repeated for all the rows of SUBS.”

    This means you can’t even fake it out by using cells.

    However, if you put the IDs in their own index variable, and then reshape your data so that data corresponding to different IDs are in different columns, this problem can be efficiently handled by bsxfun(). For reference, I also included a matrix math method, a simple for loop method using cov(), and a cellfun() method using a custom fncovariance() function (note I modified it from yours above).

    fncovariance = @(x,y) mean(x.*y) - mean(x)*mean(y);
    
    IDs = unique(mat1(:,1));
    ret = reshape(mat1(:,2), sz, length(IDs));
    idx = idx(1:sz, 2);
    
    % bsxfun method
    mean(bsxfun(@times, ret, idx)) - bsxfun(@times, mean(ret), mean(idx))
    
    % matrix math
    idx' * ret / length(idx) - mean(ret)*mean(idx)
    
    % for loop method
    id_cov = zeros(1, length(IDs));
    for i=1:length(IDs)
        tmp = cov(ret(:,i), idx, 1);
        id_cov(i) = tmp(2,1);
    end
    id_cov
    
    % cellfun method
    ret_cell = num2cell(ret, 1);
    idx_cell = num2cell(repmat(idx, 1, length(IDs)), 1);
    cellfun(fncovariance, ret_cell, idx_cell)
    

    If you simulate some more data and time these different methods, the bsxfun() way is the fastest:

    sz    = 10;
    n_ids = 100;
    
    IDs = 1:n_ids;
    ret = randi(1000, sz, n_ids);
    idx = randi(1000, sz, 1);
    
    Elapsed time is 0.001292 seconds.
    Elapsed time is 0.001523 seconds.
    Elapsed time is 0.009625 seconds.
    Elapsed time is 0.011454 seconds.
    

    A final option you might be interested in is the grpstats() function in the statistics toolbox, which lets you sweep out arbitrary statistics based on a grouping variable.

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

Sidebar

Related Questions

I am not very comfortable using javascript so trying my hands to use this
I want to try the latest VS2010 RC. But I am not very comfortable
Not very technical, but... I have to implement a bad words filter in a
I'm comfortable using git, and I prefer it very much over CVS. Unfortunately, in
Timer Tasks in Java EE are not very comfortable. Is there any util, to
I am not very good with Regex but I am learning. I would like
I'm not very good at C, and I always get stuck on simple string
I am not very familiar with databases and the theories behind how they work.
I am not very familiar with databases and what they offer outside of the
I am generally not very fond of refactoring tools. No need to get into

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.