check the repeated values...count them and find mean

I have suppose 2 columns as follows:
1 22
2 44
1 88
5 100
7 200
2 600
8 45
9 76
7 24
4 54
what i want is: like 1 is repeated 2 times...now add the respective values from column 2 i.e 22 and 88 and add them and divide by 2(because 1 is occuring 2 times)..and do it for all values of column 1..i.e
1 (22+88)/2
2 (44+600)/2
5 (100)/1
7 (200+24)/2
8 (45)/1
9 (76)/1
4 (54)/1
please help me...i m stuck up with this problem... :(

 Accepted Answer

The 'tm' you show in your code has only one column. When you write tm(:,2), matlab will complain. It looks as if you intended to join 'tm' and 's' together to form a two-columned 'tm'. Either that, or the code I wrote should use tm and s instead of tm(:,1) and tm(:,2), respectively.

6 Comments

load uprm.dat
a=uprm(:,2);
g=uprm(:,1);
tm=[];
s=[];
z=[];
sm=[];
for i = 1:length(a)
for j = i+1:length(a)
tm=[tm; (g(j)- g(i))];
s=[s; (a(i)-a(j))^2];
z=[z; tm s];
end;
end
[u,~,ix] = unique(z(:,1));
sm = [u,accumarray(ix,z(:,2))./accumarray(ix,1)];
clf;
plot(sm(:,1),sm(:,2), ':')
now is it correct?? i made a new array of two columns with 1st one tm and 2nd one s
aditi
aditi on 29 Jun 2013
Edited: aditi on 29 Jun 2013
error is gone...but its not giving the correct result... :-( is accumarray adding also the values from column 2 common to column 1?? should i explain what i have to do again...?? also please once explain me what the commands are doing so that i can check whether i am doing right or not...
Let's take the original example you gave in the beginning.
tm s ix
1 22 1
2 44 2
1 88 1
5 100 4
7 200 5
2 600 2
8 45 6
9 76 7
7 24 5
4 54 3
If you do
[u,~,ix] = unique(tm);
v = accumarray(ix,s)./accumarray(ix,1);
you should get the 'ix' above and the values below:
u accumarray(ix,s) accumarray(ix,1) v
1 110 2 55
2 644 2 322
4 54 1 54
5 100 1 100
7 224 2 112
8 45 1 45
9 76 1 76
As you see, accumarray uses the index ix to determined where to add the corresponding value in s in the first case or the value 1 in the second case. The ix index vector has one entry for each one in tm and points to the position of that unique value in u.
waoo...thnx roger...thnx a lot for the explaination...it was vry helpful...one more question...what does accumarray(ix,1) means?? i mean how is it different from accumarray(ix,s)??
This is a quote from Mathworks' documentation for 'accumarray': "val can also be a scalar whose value is repeated for all the rows of subs". The 'val' here refers to the second argument in 'accumarray'. In your case it was the number 1, so a 1 is repeated for each of the elements in 'ix' in order to count the number of occurrences of each distinct number in 'ix', which is needed for computing the mean.
Jan is correct that you could use the function "@mean" instead of the method I gave. I didn't mention it to you because I wanted the code's algorithm to be easier to understand.
Thanx a lot again Roger...it was vry helpful... :-)

Sign in to comment.

More Answers (2)

Call your first array 'a' and the second 'b'.
[u,~,ix] = unique(a(:,1));
b = [u,accumarray(ix,a(:,2))./accumarray(ix,1)];
These are arranged in order of ascending unique values from the first column of a. If you want them in the order first encountered, you need to use the second argument of 'unique'.

1 Comment

Hey roger...thanx a lot for replying... I am new to MATLAB...so please could you explain me the above two commands... where are we adding the common values respective values from array 1's second column

Sign in to comment.

Jan
Jan on 29 Jun 2013
Edited: Jan on 29 Jun 2013
One accumarray() call is enough:
A = [1 22; ...
2 44; ...
1 88; ...
5 100; ...
7 200; ...
2 600; ...
8 45; ...
9 76; ...
7 24; ...
4 54];
B = accumarray(A(:,1), A(:,2), [], @mean)
I'm not new to Matlab. But this is the first time I could apply accumarray successfully.

4 Comments

thnx jan...one question...i have to apply this command aftr unique command right???because i have to plot between unique values of 1st column of A and the column which we get after mean..
Jan when I am running ur code..its giving following error...
First input SUBS must contain positive integer subscripts.
Error in unii (line 20)
sm = accumarray(zz(:,1), zz(:,2), [], @mean)
@aditi: No, you do not need the unique command. And when you run "my code" you do not get this error message. You get the error, when the first column contain values, which are not positive integers. But your example did not show such values. So obvious your example was a too strong simplification. In this case you need.
[dum1, dum2, index] = unique(zz(:,1));
sm = accumarray(index, zz(:,2), [], @mean)
This is very similar to Roger's code, it only let accumarray perform the calculation of the mean, while Roger's accumarray builds the sum and divides by the number of occurrences determined by a second accumarray.
thnxx a lot Jan...its working now... :-)

Sign in to comment.

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!