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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:13:02+00:00 2026-05-31T19:13:02+00:00

So, presume a matrix like so: 20 2 20 2 30 2 30 1

  • 0

So, presume a matrix like so:

20 2
20 2
30 2
30 1
40 1
40 1

I want to count the number of times 1 occurs for each unique value of column 1. I could do this the long way by [sum(x(1:2,2)==1)] for each value, but I think this would be the perfect use for the UNIQUE function. How could I fix it so that I could get an output like this:

20 0
30 1
40 2

Sorry if the solution seems obvious, my grasp of loops is very poor.

  • 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-31T19:13:04+00:00Added an answer on May 31, 2026 at 7:13 pm

    Indeed unique is a good option:

    u=unique(x(:,1))
    res=arrayfun(@(y)length(x(x(:,1)==y & x(:,2)==1)),u)
    

    Taking apart that last line:

    1. arrayfun(fun,array) applies fun to each element in the array, and puts it in a new array, which it returns.
    2. This function is the function @(y)length(x(x(:,1)==y & x(:,2)==1)) which finds the length of the portion of x where the condition x(:,1)==y & x(:,2)==1) holds (called logical indexing). So for each of the unique elements, it finds the row in X where the first is the unique element, and the second is one.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let presume we have something like this: <div1> <h1>text1</h1> <h1>text2</h1> </div1> <div2> <h1>text3</h1> </div2>
Let's presume that I have string like '=&?/;#+%' to be a part of my
I know this is possible to be done using javascript, and I presume I
Remove first comma on each line in a file. I presume sed is needed.
I have a curl class, called Curl. Let's presume i have this code: $url
Let us presume we have k sequences of fixed length p . Each sequence
does anyone know how to do this? I presume it uses some sort of
What is the highest number Python 2.6's x86 id() function can return? I presume
So I presume this is a memory issue, here's the code: - (NSString *)giveMeAStringGoddammit
I want to make a sequence of in-memory operations atomic. I presume there is

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.