What is the operation of finding out how many times a number is contained in another number?

Skip to content

Given a array, is there any way to count the number of times a value occurs within a specific row of that array?

For example, if I have a array:

A=[1,2,5,2,3,4,2; 4,2,1,5,3,2,3; 1,4,2,3,2,2,1];

I want to know how many times the value '2' occurs in the second row

What is the operation of finding out how many times a number is contained in another number?

Edited: Azzi Abdelmalek on 17 Jul 2014

A=[1,2,5,2,3,4,2; 4,2,1,5,3,2,3; 1,4,2,3,2,2,1]

What is the operation of finding out how many times a number is contained in another number?

In general, you can use histc() to find the counts for all of the numbers in one shot:

A=[1,2,5,2,3,4,2; 4,2,1,5,3,2,3; 1,4,2,3,2,2,1];

counts = histc(A(:), edges)

What is the operation of finding out how many times a number is contained in another number?

Try the following to find the number of times '2' occurs in the second row


This page covers the basics of Division (÷).

See our other arithmetic pages for discussion and examples of: Addition (+), Subtraction (-) and Multiplication (×).

The usual written symbol for division is (÷). In spreadsheets and other computer applications the ‘/’ (forward slash) symbol is used.

Division is the opposite of multiplication in mathematics.

Division is often considered the most difficult of the four main arithmetic functions. This page explains how to perform division calculations. Once we have a good understanding of the method and rules, we can use a calculator for more tricky calculations without making mistakes.

Division allows us to divide or 'share' numbers to find an answer. For example, let’s consider how we would find the answer to 10 ÷ 2 (ten divided by two). This is the same as ‘sharing’ 10 sweets between 2 children. Both children must end up with the same number of sweets. In this example the answer is 5.

Some Quick Rules about Division:

  • When you divide 0 by another number the answer is always 0. For example: 0 ÷ 2 = 0. That is 0 sweets shared equally among 2 children - each child gets 0 sweets.

  • When you divide a number by 0 you are not dividing at all (this is quite a problem in mathematics). 2 ÷ 0 is not possible. You have 2 sweets but no children to divide them among. You cannot divide by 0.

  • When you divide by 1, the answer is the same as the number you were dividing. 2 ÷ 1 = 2. Two sweets divided by one child.

  • When you divide by 2 you are halving the number. 2 ÷ 2 = 1.

  • Any number divided by the same number is 1. 20 ÷ 20 = 1. Twenty sweets divided by twenty children - each child gets one sweet.

  • Numbers must be divided in the correct order. 10 ÷ 2 = 5 whereas 2 ÷ 10 = 0.2. Ten sweets divided by two children is very different to 2 sweets divided by 10 children.

  • All fractions such as ½, ¼ and ¾ are division sums. ½ is 1 ÷ 2. One sweet divided by two children. See our page Fractions for more information.

Just as multiplication is a quick way of calculating multiple additions, division is a quick way of performing multiple subtractions.

For example:

If John has 10 gallons of fuel in his car and uses 2 gallons a day how many days before he runs out?

We can work this problem out by doing a series of subtractions, or by counting backwards in steps of 2.

  • On day 1 John starts with 10 gallons and ends with 8 gallons.  10 - 2 = 8
  • On day 2 John starts with 8 gallons and ends with 6 gallons. 8 - 2 = 6
  • On day 3 John starts with 6 gallons and ends with 4 gallons. 6 - 2 = 4
  • On day 4 John starts with 4 gallons and ends with 2 gallons. 4 - 2 = 2
  • On day 5 John starts with 2 gallons and ends with 0 gallons. 2 - 2 = 0

John runs out of fuel on day 5. 

A quicker way of performing this calculation would be to divide 10 by 2. That is, how many times does 2 go into 10, or how many lots of two gallons are there in ten gallons? 10 ÷ 2 = 5.

The multiplication table (see multiplication) can be used to help us find the answer to simple division calculations.

In the example above we needed to calculate 10 ÷ 2. To do this, using the multiplication table locate the column for 2 (the red shaded heading).  Work down the column until you find the number you are looking for, 10. Move across the row to the left to see the answer (the red shaded heading) 5.

× 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100

We can work out other simple division calculations using the same method. 56 ÷ 8 = 7 for example. Find 7 on the top row, look down the column until you find 56, then find the corresponding row number, 8.

If possible, you should try to memorise the multiplication table above because it makes solving simple multiplication and division calculations much quicker.

Dividing Larger Numbers

You can use a calculator to perform division calculations, especially when you are dividing larger numbers that are more difficult to work out in your head. However, it is important to understand how to perform division calculations manually. This is helpful when you don’t have a calculator to hand, but is also essential for making sure that you use the calculator correctly and don’t make mistakes. Division can look daunting but in fact, as with most arithmetic,  it is logical.

As with all mathematics, it is easiest to understand if we work through an example:

Dave’s car needs new tyres. He needs to replace all four tyres on the car, plus the spare.

Dave has had a quote from a local garage for £480 to include the tyres, fitting and disposal of the old tyres. How much does each tyre cost?

The problem we need to calculate here is 480 ÷ 5. This is the same as saying how many times will 5 go into 480?

Conventionally, we write this as:


We work from left to right in a logical system.

We start by dividing 4 by 5 and immediately hit a problem. 4 does not divide by 5 to leave a whole number, as 5 is greater than 4.

The language we use in maths can be confusing. Another way of looking at this is to say, ‘how many times does 5 go into 4?’.

We know that 2 goes into 4 twice (4 ÷ 2 = 2) and we know that 1 goes into 4 four times (4 ÷ 1 = 4), but 5 does not go into 4 because 5 is larger than 4.

The number we are dividing by (in this case 5) needs to go into the number we are dividing into (in this case 4) a whole number of times. It doesn’t have to be an exact whole number, as you will see.

Since 5 does not go into 4 we put a 0 in the first (hundreds) column. For help with the hundreds, tens and units columns see our page on numbers.

Hundreds Tens Units
0    
5 4 8 0

Next, we move to the right to include the tens column. Now we can see how many times 5 goes into 48.

5 does go into 48 as 48 is greater than 5. However, we need to find out how many times it goes.

If we refer to our multiplication table, we can see that 9 × 5 = 45 and 10 × 5 = 50.

48, the number we’re looking for, falls between these two values. Remember, we are interested in the whole number of times that 5 goes into 48. Ten times is too many.

We can see that 5 goes into 48 a whole number (9) times, but not exactly, with 3 left over.

9 × 5 = 45
48 – 45 = 3

We can now say that 5 goes into 48 nine times, but with a remainder of 3. The remainder is what is left when we subtract the number we have found from the number we are dividing into: 48 - 45 = 3.

So 5 × 9 = 45, + 3 to get 48.

We can enter 9 in the tens column as our answer for the second part of the calculation and bring our remainder in front of our last number in the units column. Our last number becomes 30.

Hundreds Tens Units
0 9  
5 4 8 30

We now divide 30 by 5 (or find out how many times 5 goes into 30). Using our multiplication table we can see the answer is exactly 6, with no remainder. 5 × 6 = 30. We write 6 in the units column of our answer.

Hundreds Tens Units
0 9 6
5 4 8 30

As there are no remainders, we have finished the calculation and have the answer 96.

Dave’s new tyres are going to cost £96 each. 480 ÷ 5 = 96 and 96 × 5 = 480.

Recipe Division

Our final example of division is based on a recipe.  Often when cooking, recipes will tell you how much food they are going to make, enough to feed 6 people, for example.

The ingredients below are needed to make 24 fairy cakes, however, we only want to make 8 fairy cakes. We have modified the ingredients slightly for the benefit of this example (original recipe at: BBC Food).

The first thing we need to establish is how many 8's there are in 24 – use the multiplication table above or your memory.  3 × 8 = 24 – if we divide 24 by 8 we get 3. Therefore we need to divide each ingredient below by 3 in order to have to right amount of mixture to make 8 fairy cakes.

Ingredients

  • 120g butter, softened at room temperature
  • 120g caster sugar
  • 3 free-range eggs, lightly beaten
  • 1 tsp vanilla extract
  • 120g self-raising flour
  • 1-2 tbsp milk

The amount of butter, sugar and flour are all the same, 120g.  It is therefore only necessary to work out 120 ÷ 3 once, as the answer will be the same for those three ingredients.


As before we start in the left (hundreds) column and divide 1 by 3. However 3 ÷ 1 doesn’t go as 3 is greater than 1. Next, we look at how many times 3 goes into 12. Using the multiplication table if needed we can see that 3 goes into 12 exactly 4 times with no remainder.


120g ÷ 3 is therefore 40g. We now know that we’ll need 40g of butter, sugar and flour.

The original recipe calls for 3 eggs and we again divide by 3. So 3 ÷ 3 = 1, therefore one egg is needed.

Next the recipe calls for 1tsp (teaspoon) of vanilla extract. We need to divide one teaspoon by 3. We know that division can be written as a fraction, so 1 ÷ 3 is the same as ⅓ (one third). You’ll need ⅓ of a teaspoon of vanilla extract – although in reality it may be difficult to accurately measure ⅓ of a teaspoon!

Estimating can be useful, and units can be changed!

We can look at this another way, if we know that one teaspoon is the same as 5ml or 5 millilitres. (If you need some help with units, see our page on Systems of Measurement.) If we want to be more accurate, we can try dividing 5ml by 3. 3 goes into 5 once (3) with 2 left over. 2 ÷ 3 is the same as ⅔, so 5ml divided by 3 gives us 1⅔ml, which in decimals is 1.666ml. We can use our estimating skills and say that one teaspoon divided by three is a tiny bit more than one and a half ml. If you have some of those tiny measuring spoons in your kitchen, you can be super-accurate!

We can estimate the answer, to check that we are correct. Three lots of 1.5 ml gives us 4.5 ml. So three lots of ‘a tiny bit more than 1.5 ml’, gives us around 5ml. Recipes are rarely an exact science, so a little bit of estimating can be fun and good practice for our mental arithmetic.


Next the recipe calls for 1–2 tbsp of milk. That is between 1 and 2 tablespoons of milk. We have no definitive amount and how much milk you add will be dependent on your mixture consistency.

We already know that 1 ÷ 3 is ⅓ and 2 ÷ 3 is ⅔. We will therefore need ⅓–⅔ of a tablespoon of milk to make eight fairy cakes. Let’s look at this another way. One tablespoon is the same as 15ml. 15 ÷ 3 = 5, so ⅓–⅔ of a tablespoon is the same as 5–10ml, which is the same as 1–2 teaspoons!


What is the operation of finding out how many times a number is contained in another number?

Further Reading from Skills You Need

Fundamentals of Numeracy
Part of The Skills You Need Guide to Numeracy

This eBook provides worked examples and easy-to-understand explanations to show you how to use basic mathematical operations and start to manipulate numbers. It also includes real-world examples to make clear how these concepts are useful in real life.

Whether you want to brush up on your basics, or help your children with their learning, this is the book for you.