Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to generate 2D Frequency Matrix

Subject: How to generate 2D Frequency Matrix

From: S Ehtesham Al Hanif

Date: 2 Dec, 2012 18:19:08

Message: 1 of 9

I have a matrix A( i x j) like this which has row=i=1400 numbers and column=j=2.

As this matrix both column vectors has repetition of no such as 1's 15 times, 2's 18 times etc. So, I want to transfer that matrix into a single matrix which will be a frequency matrix of that one.

Such as: if i think about a table then on column i divided it into (1 to 3, 3-5, 5-7) like that and row ways (1 to 5, 5-10, 10-15) and which matrix i want that should fulfill both condition in a same row at the same time and count how many times it repeats and put that on that space or location.

Suppose, If i have row ways 10 group and column ways 5 groups then my final frequency matrix will be (10 x 5)

Please If anybody help me to sort it out! It will be great help for me!

Subject: How to generate 2D Frequency Matrix

From: dpb

Date: 2 Dec, 2012 21:09:06

Message: 2 of 9

On 12/2/2012 12:19 PM, S Ehtesham Al Hanif wrote:
> I have a matrix A( i x j) like this which has row=i=1400 numbers and
> column=j=2.
>
> As this matrix both column vectors has repetition of no such as 1's 15
> times, 2's 18 times etc. So, I want to transfer that matrix into a
> single matrix which will be a frequency matrix of that one.
>
> Such as: if i think about a table then on column i divided it into (1 to
> 3, 3-5, 5-7) like that and row ways (1 to 5, 5-10, 10-15) and which
> matrix i want that should fulfill both condition in a same row at the
> same time and count how many times it repeats and put that on that space
> or location.
>
> Suppose, If i have row ways 10 group and column ways 5 groups then my
> final frequency matrix will be (10 x 5)
>
> Please If anybody help me to sort it out! It will be great help for me!

I can't follow your description but look at

doc histc % and friends...

--

Subject: How to generate 2D Frequency Matrix

From: dpb

Date: 2 Dec, 2012 21:15:37

Message: 3 of 9

On 12/2/2012 3:09 PM, dpb wrote:
...

> I can't follow your description but look at
>
> doc histc % and friends...

Oh, and

doc unique % will probably be useful to identify the bins

--

Subject: How to generate 2D Frequency Matrix

From: Roger Stafford

Date: 2 Dec, 2012 22:09:08

Message: 4 of 9

"S Ehtesham Al Hanif" <ehtesham.buet@gmail.com> wrote in message <k9g62s$h7b$1@newscl01ah.mathworks.com>...
> I have a matrix A( i x j) like this which has row=i=1400 numbers and column=j=2.
>
> As this matrix both column vectors has repetition of no such as 1's 15 times, 2's 18 times etc. So, I want to transfer that matrix into a single matrix which will be a frequency matrix of that one.
>
> Such as: if i think about a table then on column i divided it into (1 to 3, 3-5, 5-7) like that and row ways (1 to 5, 5-10, 10-15) and which matrix i want that should fulfill both condition in a same row at the same time and count how many times it repeats and put that on that space or location.
>
> Suppose, If i have row ways 10 group and column ways 5 groups then my final frequency matrix will be (10 x 5)
>
> Please If anybody help me to sort it out! It will be great help for me!
- - - - - - - - - -
  If I understand you correctly, you want to produce a "frequency" matrix B from A such that in the i-th row and j-th column of B you have a total count of the number of row-pairs from A which satisfy the condition that the first element of the pair falls in the i-th interval of the first type of grouping and the second element in the j-th interval of the second type of grouping. In each grouping the intervals are all to be of the same length, (an important assumption in what follows.) Is that interpretation correct?

  If so, you need to define your two types of groupings. Let u1 and d1 define the first grouping of n1 intervals by

 1st interval: u1 <= a1 < u1+d1
 2nd interval: u1+d1 <= a1 < u1+2*d1
 3rd interval: u1+2*d1 <= a1 < u1+3*d1
 ...
 n1-st interval: u1+(n1-1)*d1 <= a1 < u1+n1*d1

where a1 is an element of the first column of A. Similarly let u2 and d2 define the second group of n2 intervals by

 1st interval: u2 <= a2 < u2+d2
 ...
 n2-nd interval: u2+(n2-1)*d2 <= a2 < u2+n2*d2

where a2 is the corresponding second element of a pair from A.

  Defining u1, d1, n1, u2, d2, and n2 is your task. In the method that follows it is important that these be defined such that the two elements of each pair from A fall somewhere within the above two types of intervals with the upper "less than" inequalities strictly satisfied.

  Then do this:

 B = accumarray([floor((A(:,1)-u1)/d1+1),floor((A(:,2)-u2)/d2+1)],1,[n1,n2]);

This should give you an n1 by n2 matrix of pair counts.

Roger Stafford

Subject: How to generate 2D Frequency Matrix

From: S Ehtesham Al Hanif

Date: 3 Dec, 2012 06:43:08

Message: 5 of 9

"Roger Stafford" wrote in message <k9gji4$mg$1@newscl01ah.mathworks.com>...
> "S Ehtesham Al Hanif" <ehtesham.buet@gmail.com> wrote in message <k9g62s$h7b$1@newscl01ah.mathworks.com>...
> > I have a matrix A( i x j) like this which has row=i=1400 numbers and column=j=2.
> >
> > As this matrix both column vectors has repetition of no such as 1's 15 times, 2's 18 times etc. So, I want to transfer that matrix into a single matrix which will be a frequency matrix of that one.
> >
> > Such as: if i think about a table then on column i divided it into (1 to 3, 3-5, 5-7) like that and row ways (1 to 5, 5-10, 10-15) and which matrix i want that should fulfill both condition in a same row at the same time and count how many times it repeats and put that on that space or location.
> >
> > Suppose, If i have row ways 10 group and column ways 5 groups then my final frequency matrix will be (10 x 5)
> >
> > Please If anybody help me to sort it out! It will be great help for me!
> - - - - - - - - - -
> If I understand you correctly, you want to produce a "frequency" matrix B from A such that in the i-th row and j-th column of B you have a total count of the number of row-pairs from A which satisfy the condition that the first element of the pair falls in the i-th interval of the first type of grouping and the second element in the j-th interval of the second type of grouping. In each grouping the intervals are all to be of the same length, (an important assumption in what follows.) Is that interpretation correct?
>
> If so, you need to define your two types of groupings. Let u1 and d1 define the first grouping of n1 intervals by
>
> 1st interval: u1 <= a1 < u1+d1
> 2nd interval: u1+d1 <= a1 < u1+2*d1
> 3rd interval: u1+2*d1 <= a1 < u1+3*d1
> ...
> n1-st interval: u1+(n1-1)*d1 <= a1 < u1+n1*d1
>
> where a1 is an element of the first column of A. Similarly let u2 and d2 define the second group of n2 intervals by
>
> 1st interval: u2 <= a2 < u2+d2
> ...
> n2-nd interval: u2+(n2-1)*d2 <= a2 < u2+n2*d2
>
> where a2 is the corresponding second element of a pair from A.
>
> Defining u1, d1, n1, u2, d2, and n2 is your task. In the method that follows it is important that these be defined such that the two elements of each pair from A fall somewhere within the above two types of intervals with the upper "less than" inequalities strictly satisfied.
>
> Then do this:
>
> B = accumarray([floor((A(:,1)-u1)/d1+1),floor((A(:,2)-u2)/d2+1)],1,[n1,n2]);
>
> This should give you an n1 by n2 matrix of pair counts.
>
> Roger Stafford

Hi Roger

Actually, I have data’s like below:
A B
4.8 1.34
9.5 1.30
13.8 1.21
18.5 1.30
23.0 1.25
27.2 1.16
27.9 0.18
29.1 0.36
33.3 1.16
34.9 0.45
36.1 0.31
36.2 0.04
35.6 -0.18
34.6 -0.27
33.6 -0.27
32.8 -0.22
31.9 -0.27
27.4 -1.25
24.0 -0.94
24.0 0.00

Now I want to generate a matrix with frequency distribution like below
A\B -1 -2 0 1 2
0-5 x x x x x
5-10 x x x x x
10-15 x x x x x
15-20 x x x x x

These x (frequency) I have to find out and these, x will be a (mxn) matrix based on those condition. Each x will have to fulfill A(0-5,5-10 etc) & B(-1,-2,0, etc) matrix at a same time then when full column matrix complete then the resultant X(mxn) matrix with overall frequency.

Subject: How to generate 2D Frequency Matrix

From: Roger Stafford

Date: 3 Dec, 2012 07:12:10

Message: 6 of 9

"S Ehtesham Al Hanif" <ehtesham.buet@gmail.com> wrote in message <k9hhls$6gh$1@newscl01ah.mathworks.com>...
> Actually, I have data’s like below:
> A B
> 4.8 1.34
> 9.5 1.30
> 13.8 1.21
> 18.5 1.30
> 23.0 1.25
> 27.2 1.16
> 27.9 0.18
> 29.1 0.36
> 33.3 1.16
> 34.9 0.45
> 36.1 0.31
> 36.2 0.04
> 35.6 -0.18
> 34.6 -0.27
> 33.6 -0.27
> 32.8 -0.22
> 31.9 -0.27
> 27.4 -1.25
> 24.0 -0.94
> 24.0 0.00
>
> Now I want to generate a matrix with frequency distribution like below
> A\B -1 -2 0 1 2
> 0-5 x x x x x
> 5-10 x x x x x
> 10-15 x x x x x
> 15-20 x x x x x
>
> These x (frequency) I have to find out and these, x will be a (mxn) matrix based on those condition. Each x will have to fulfill A(0-5,5-10 etc) & B(-1,-2,0, etc) matrix at a same time then when full column matrix complete then the resultant X(mxn) matrix with overall frequency.
- - - - - - - - - -
  I don't understand your example. For instance, which of the 20 x's does the pair (36.2 , 0.04) contribute to? Your A column values go beyond the intervals you describe and the B conditions aren't intervals.

Roger Stafford

Subject: How to generate 2D Frequency Matrix

From: Bruno Luong

Date: 3 Dec, 2012 07:33:09

Message: 7 of 9

"S Ehtesham Al Hanif" <ehtesham.buet@gmail.com> wrote in message <k9g62s$h7b$1@newscl01ah.mathworks.com>...
> I have a matrix A( i x j) like this which has row=i=1400 numbers and column=j=2.
>
> As this matrix both column vectors has repetition of no such as 1's 15 times, 2's 18 times etc. So, I want to transfer that matrix into a single matrix which will be a frequency matrix of that one.
>
> Such as: if i think about a table then on column i divided it into (1 to 3, 3-5, 5-7) like that and row ways (1 to 5, 5-10, 10-15) and which matrix i want that should fulfill both condition in a same row at the same time and count how many times it repeats and put that on that space or location.
>
> Suppose, If i have row ways 10 group and column ways 5 groups then my final frequency matrix will be (10 x 5)
>
> Please If anybody help me to sort it out! It will be great help for me!

Try this:

http://www.mathworks.com/matlabcentral/fileexchange/23897-n-dimensional-histogram/content/histcn.m

[count] = histcn(data, [1 5 10 15], [1 3 5 7])

% Bruno

Subject: How to generate 2D Frequency Matrix

From: S Ehtesham Al Hanif

Date: 3 Dec, 2012 08:00:09

Message: 8 of 9

"Roger Stafford" wrote in message <k9hjca$bro$1@newscl01ah.mathworks.com>...
> "S Ehtesham Al Hanif" <ehtesham.buet@gmail.com> wrote in message <k9hhls$6gh$1@newscl01ah.mathworks.com>...
> > Actually, I have data’s like below:
> > A B
> > 4.8 1.34
> > 9.5 1.30
> > 13.8 1.21
> > 18.5 1.30
> > 23.0 1.25
> > 27.2 1.16
> > 27.9 0.18
> > 29.1 0.36
> > 33.3 1.16
> > 34.9 0.45
> > 36.1 0.31
> > 36.2 0.04
> > 35.6 -0.18
> > 34.6 -0.27
> > 33.6 -0.27
> > 32.8 -0.22
> > 31.9 -0.27
> > 27.4 -1.25
> > 24.0 -0.94
> > 24.0 0.00
> >
> > Now I want to generate a matrix with frequency distribution like below
> > A\B -1 -2 0 1 2
> > 0-5 x x x x x
> > 5-10 x x x x x
> > 10-15 x x x x x
> > 15-20 x x x x x
> >
> > These x (frequency) I have to find out and these, x will be a (mxn) matrix based on those condition. Each x will have to fulfill A(0-5,5-10 etc) & B(-1,-2,0, etc) matrix at a same time then when full column matrix complete then the resultant X(mxn) matrix with overall frequency.
> - - - - - - - - - -
> I don't understand your example. For instance, which of the 20 x's does the pair (36.2 , 0.04) contribute to? Your A column values go beyond the intervals you describe and the B conditions aren't intervals.
>
> Roger Stafford

Hi Roger

Actually in my case, I have three column but i can avoid first one which has time value. In details:
Column T has value of time as second t=1:1:1400;
Column A has value of velocity/speed and it has 1400 speed values from 0(zero) to 100(hundred). In this case some velocity values repeats after uncertain interval.
Column B has value of acceleration and it has 1400 acceleration/deccelaration values from --4(minus four) to +4(four). In this case some acceleration values has repetition.

Now i want to generate a mxn matrix where every position will be a frequency which can full fill one condition from A and same time one corresponding B condition.

I subdivided velocity column into several group, such as: 0 to 5, 5 to 10, 10 to 15, 15 to 20 .........90 to 95 and 95-100.

Similarly i subdivided acceleration column into several group such as: -4 to -3, -3 to -2 ...... 2 to 3 and 3 to 4.

Then, i will count frequency such a way that it will full fill both columns same row with those condition.

As an example: if i look at the previous posts Column A. And Column B, then i have to find out how many times/frequency the below conditioned fullfilled?

If velocity range : 30 to 35 m/s and acceleration at the same time/row in -0.5 to -0.25 range ? According to example it has frequent/count 3! If velocity range same but acceleration condition changed to -0.25 to 0 then it has frequency 1.

I think this time my illustration is better as if it is possible to show you by showing picture or figure by attaching here it will be easy to explain what i am planning for!

Ehtesham

Subject: How to generate 2D Frequency Matrix

From: Roger Stafford

Date: 3 Dec, 2012 13:54:08

Message: 9 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k9hkjl$fle$1@newscl01ah.mathworks.com>...
> Try this:
>
> http://www.mathworks.com/matlabcentral/fileexchange/23897-n-dimensional-histogram/content/histcn.m
>
> [count] = histcn(data, [1 5 10 15], [1 3 5 7])
>
> % Bruno
- - - - - - - - -
  That's great Bruno! That's exactly what is needed here in case of variable-length two dimensional intervals. I didn't know you had written that.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us