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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:08:22+00:00 2026-06-11T12:08:22+00:00

Suppose now I have two vectors of same length: A = [1 2 2

  • 0

Suppose now I have two vectors of same length:

A = [1 2 2 1];
B = [2 1 2 2];

I would like to create a matrix C whose dim=m*n, m=max(A), n=max(B).

C = zeros(m,n);
for i = 1:length(A)
   u = A(i);
   v = B(i);
   C(u,v)=C(u,v)+1;
end

and get

C =[0 2;
    1 1]

More precisely, we treat the according indices in A and B as rows and columns in C, and C(u,v) is the number of elements in {k | A(i)=u and B(i)=v, i = 1,2,…,length(A)}

Is there a faster way to do that?

  • 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-11T12:08:24+00:00Added an answer on June 11, 2026 at 12:08 pm

    Yes. Use sparse. It assembles (i.e., sums up) the matrix values for repeating row-column pairs for you. You need an additional vector with the values that will be assembled into the matrix entries. If you use ones(size(A)), you will have exactly what you need – counting of repeated row-column pairs

    spA=sparse(A, B, ones(size(A)));
    full(spA)
    
    ans =
    
     0     2
     1     1
    

    The same can be obtained by simply passing scalar 1 to sparse function instead of a vector of values.

    For matrices that have a large number of zero entries this is absolutely crucial that you use sparse storage. Another function you could use is accumarray. It can essentially do the same thing, but also works on dense matrix structure:

    AA=accumarray([A;B]', 1);
    
    AA =
    
     0     2
     1     1
    

    You can pass size argument to accumarray if you want to create a matrix of specific size

    AA=accumarray([A;B]', 1, [2 3]);
    AA =
    
     0     2     0
     1     1     0
    

    Note that you can actually also make it produce sparse matrices, and use a different operator in assembly (i.e., not necessarily a sum)

    AA=accumarray([A;B]', 1, [2 3], @sum, 0, true)
    

    will produce a sparse matrix (last parameter set to true) using sum for assembly and 0 as a fill value, i.e. a value which is used in cases a given row-column pair does not exist in A/B.

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

Sidebar

Related Questions

I have two Tables Let suppose A and B Now suppose the structure of
Suppose I have two models A and B Now I want to copy some
Suppose I have two data frame 'df_a' & 'df_b' , both have the same
Suppose I have two lists, L and M. Now I want to know if
suppose I have two columns of integers, A and B. Now I want distinct
I have a following question. Now, suppose we have two repositories, a stable one
this is a follow-up question of mine. Suppose now I have a URL :
Suppose I have a sting as PHP Paddy,PHP Pranav,PHP Parth, now i have a
Suppose I have this feature branch foo. Now I want to merge it back
Suppose i have 1kk records in my database. Now i need to select 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.