# Matlab

• Nov 14th 2007, 09:38 AM
tauhu82
Matlab
hi, i would like to ask how can i do with MATLAB to find the sum values from a unique data.

for example i have the data,

p=[1 1 3 1 2 1 1 1 4 1 1 0.92]
r=[0.1 0.2 0.3 0.3 0.3 0.5 0.6 0.7 0.8 0.8 0.9 0.91]

as we can see there are three 0.3 in set r, and i would like to get the sum value of P ((3+1+2)) where r= 0.3, and the sum value of p ((4+1)) where r=0.8. while for the other P values with the distinctive r value will remain the same.

thanks.
• Nov 14th 2007, 12:47 PM
qspeechc
I am sorry, I am not quite sure I understand- what exactly are youtrying to do?
Are we talking about P or p? What is P? MATLAB distinguishes between lower and upper case, ie. P is NOT the same thing as p. What has p to do with r? Are you trying to sum all the elements of the vector? Or count how many times a specific number occurs?
• Nov 14th 2007, 06:41 PM
tauhu82
sorry for the confusion. Actually all are lower case p.

p r (in file PR.mat)
1 0.1
1 0.2
1 0.3
1 0.3
1 0.4
1 0.5
1 0.6
1 0.7
1 0.8
1 0.8
1 0.9
0.92 0.9
0.92 0.91

How do i isolate out the above data so that r will only have
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.91 and if the data for
r repeat (for example 0.3, 0.8, 0.9) then i will
have to get the sum of its p data.

for example, i would like to have this result

p r
1 0.1
1 0.2
2 0.3
1 0.4
1 0.5
1 0.6
1 0.7
2 0.8
1.92 0.9
0.92 0.91

this is what i tried,

r;
uR=unique(r); %%this is to get the unique num of set r (0.1,0.2,0.3,0.5,0.6,0.7,0.8,0.9,0.91)

for i=1:8

for loop =1:12

pp(loop)=p(loop);
psum=sum(pp)

end

end

end

but the pp i got is not according to the r. am i using the wrong way? what shall i proceed with?
thanks.
• Nov 14th 2007, 07:04 PM
tauhu82
i've gt it. thanks for replying qspeechc.
• Nov 15th 2007, 09:23 AM
qspeechc
Lol, I didn't do anything, but I'll take the thanks anyway :D