How can I sum values at specific point

7 views (last 30 days)
ak42
ak42 on 2 Jul 2014
Commented: ak42 on 3 Jul 2014
First , I have a thousand height values depending on position values. I want to order these values according to a rule. Because some height values are at the exact positon or same position. I need to sum these height values and express it with position values or plot it. For ınstance
P=[1 2 4 1 3 2 2 5]
H=[3 1 1 2 3 1 4 2]
I want these vectors
P_ordered=[1 2 3 4 5]
H_sum =[5 6 3 1 2]
Second,now, I have some other height values depends on position values named H_original and I want to do
H_original-H_sum = H_final
and plot(P,H_final,)
P=[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
H_original=[10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ]
What I want to reach
P=[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
H_final=[10 10 10 10 10 10 10 10 10 10 5 4 7 9 8 10 10 10 10 10 10 ]
Plot(P,H_final)
  1 Comment
the cyclist
the cyclist on 2 Jul 2014
I don't understand the rule for the second part of what you want to do.

Sign in to comment.

Answers (2)

the cyclist
the cyclist on 2 Jul 2014
First part:
If P is always some ordering of the numbers 1-N, then this simple code will work:
P_ordered = unique(P);
H_sum = accumarray(P',H')';
If not, can you please supply a more general example of how P might look?

Roger Stafford
Roger Stafford on 2 Jul 2014
I have called the larger P array, 'P_large', to distinguish it from the original P. The following code assumes that each element of P_ordered will match one and only one element of P_large. It also assumes that P and H are the same size, and that P_large and H_original are the same size.
P = [1 2 4 1 3 2 2 5];
H = [3 1 1 2 3 1 4 2];
P_large =[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10];
H_original=[10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10];
[P_ordered,~,iP] = unique(P);
H_sum = accumarray(iP,H,size(P_ordered));
[~,iH] = ismember(P_ordered,P_large);
H_final = H_original-accumarray(iH,H_sum,size(P_large));
Note: In your example in obtaining H_final you appear to have misaligned H_sum with H_original by one position. This code assumes this was an error.
  1 Comment
ak42
ak42 on 3 Jul 2014
it gives size problem. And another problem(because of me), accumarray works for integer. Actually I simplified my question so it doesn't work. I wıll write it more complex. Assume I have two group data, x(y') and z(y) points, and y' is member of y or in the range of y. Now the question how can find z(y)=z(y)- x(y') at y'=y.
For example
for group of calculated data
y'_1= 0.023 is x_1=3.127
y'_2= -0.043 is x_2=2.324
y'_i= 0.112 is x_i=1.3418
and
for given data
y_1=-0.047 is z_1=2.432
y_2=0.023 is z_2= 5.231
Y_3=0.05 is z_3=4.548
y_n=0.1 is z_n=3.21
i and n are dimension of data or range. I want to find or match y' values in y and then corresponding to y'=~y I want to do z=z-x
in other words if ı get y'_2 value, it is close to y_1 and then I rounded it to y_1(because y' is not match and it is not near y_2). Later I do
z_1-x_2=2.432-2.324=0.088 =z_1 corresponding to point y_1
now for y_1=-0.047 is z_1=0.088 y_2=0.023 is z_2= 5.231
Y_3=0.05 is z_3=4.548
y_n=0.1 is z_n=3.21

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!