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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:20:02+00:00 2026-06-02T01:20:02+00:00

Given two vectors r and c that hold row and column subscripts into a

  • 0

Given two vectors r and c that hold row and column subscripts into a matrix A (its size also given), I want to compute A. Points can occur multiple times and should increment the corresponding element in A for every occurence. An example:

r = [1 2 4 3 2];
c = [2 2 1 1 2];
A = zeros(4, 3);

ind = sub2ind(size(A), r, c);
for i = 1 : length(ind)
    A(ind(i)) = A(ind(i)) + 1; % could as well use r and c instead of ind ...
end

This yields the matrix

A =
     0     1     0
     0     2     0
     1     0     0
     1     0     0

I’d like to avoid the loop if possible. Is there a vectorized solution to this problem? Preferably one that does not generate huge temporary matrices …

  • 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-02T01:20:03+00:00Added an answer on June 2, 2026 at 1:20 am

    This is a job for accumarray:

    r = [1 2 4 3 2];
    c = [2 2 1 1 2];
    
    %# for every r,c pair, sum up the occurences
    %# the [4 3] is the array size you want
    %# the zero is the value you want where there is no data
    accumarray([r',c'],ones(length(r),1),[4 3],@sum,0)
    
    ans =
    
         0     1     0
         0     2     0
         1     0     0
         1     0     0
    

    Note that if your resulting array has so many zeros (i.e. is very sparse), sparse may be the better option, as suggested by @woodchips

    sparse(r,c,ones(size(r)),4,3)
    
    ans =
    
       (3,1)        1
       (4,1)        1
       (1,2)        1
       (2,2)        2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given any two sequences/vectors of M real numbers, I can easily compute their closeness
Given two model classes, Foo and Bar , I want Foo to have 3
Given two sorted arrays: A and B . The size of array A is
Given two vectors X and Y , I have to find their correlation, i.e.
Currently I have two larger vectors of 50+ strings I want to be able
I have two vectors A B which are coupled, I mean that they define
I have a function that processes a given vector, but may also create such
Given the following function which takes two parameters (csv-data1 and csv-data2, vectors of vectors
Given is an online swf on Hubworld's site, which loads two configuration XMLs into
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {

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.