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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:38:04+00:00 2026-05-16T04:38:04+00:00

I want to get better at vectorizing my loops in MATLAB. At the moment,

  • 0

I want to get better at vectorizing my loops in MATLAB. At the moment, I’m trying to count the occurrences of values in a list of ints. My code is similar to this:

list = [1 2 2 3 1 3 2 2 2 1 5];
occurrence_list = zeros(1,max(list));

for x=list
    occurrence_list(x) = occurrence_list(x) + 1;
end

Is there a simple vectorized replacement for that for loop? (Or is there a built in MATLAB function that I’m missing?) I’m doing this on pretty small data sets, so time isn’t an issue. I just want to improve my MATLAB coding style.

  • 1 1 Answer
  • 2 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-16T04:38:04+00:00Added an answer on May 16, 2026 at 4:38 am

    In addition to the HIST/HISTC functions, you can use the ACCUMARRAY to count occurrence (as well as a number of other aggregation operations)

    counts = accumarray(list(:), 1)
    %# same as: accumarray(list(:), ones(size(list(:))), [], @sum)
    

    Another way is to use TABULATE from the Statistics Toolbox (returns value,count,frequency):

    t = tabulate(list)
    t =
                1            3       27.273
                2            5       45.455
                3            2       18.182
                4            0            0
                5            1       9.0909
    

    Note that in cases where the values don’t start at 1m or in case there’s large gaps between the min and the max, you will get a lot of zeros in-between the counts. Instead use:

    list = [3 11 12 12 13 11 13 12 12 12 11 15];
    v = unique(list);
    table = [v ; histc(list,v)]'
    
    table =
         3     1
        11     3
        12     5
        13     2
        15     1
    

    representing the unique values and their counts (this will only list values with at least one occurrence)

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

Sidebar

Related Questions

I'm a C programmer and trying to get better at C++. I want to
What I want: get a xml from the AppData to use What I code
I just want get a 2 dimension array of List in c#. In my
I want to get list of tables in a given database and a given
I want to get better at JQuery, but I don't know what type of
I want to get a better understanding of how disk reads work for a
I just want to get a better understanding of what a stack is in
I want to get 1/7 with better precision, but it got truncated. How can
I am a relative newbie to hadoop and want to get a better understanding
Here's a quick one. I'm trying to get better at writing selectors for jquery

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.