Matlab How to find mutual numbers in 2 different vectors and replace them?

advertisements

Help!

How to find mutual numbers in 2 different vectors and replace them?

I have this situation:

2016    2059    43
2362    2450    88
2451    2474    25
2475    2684    36
3301    3332    31

I would like to get start and end when the colum 1 is different from the second column by 1 or zero and the column number 3 has to add the value as in the following example. im sorry for my bad explanation guys.

Here is the output that i would like to get

2016    2059    43
2362    2684    149
3301    3332    31


This answer is about as clear as your question :-)

x = [ 2016    2059    43
      2362    2450    88
      2450    2474    25
      2474    2684    36
      3301    3332    31 ];

[~, ii] = ismember(x(:,2),x(:,1));
jj = ~ii;
kk = sum(jj)+1+flipud(cumsum(flipud(-jj)));
ll = true(size(x,1),1);
ll(nonzeros(ii)) = false;
result = [ x(ll,1)  x(jj,2) accumarray(kk,x(:,3)) ];

This gives:

result =
        2016        2059          43
        2362        2684         149
        3301        3332          31