-1

I have an 2D array like the one shown below.

35046,  0.17    
1963,   0.34    
1135,   0.51    
405,    0.68    
99, 0.85    
1,  0.85    
15, 1.02    
2,  1.02

I tried using accumarray function in matlab on this data to get the results like this below.

35046,  0.17    
1963,   0.34    
1135,   0.51    
405,    0.68    
100,    0.85    
17, 1.02

The following is the code snippet for the same.

[num_dist, txt_dist]=xlsread(fname);
num_dist_final(:,1) = accumarray(num_dist(:,2),num_dist(:,1));
num_dist_final(:,2) = unique(num_dist(:,2));

But accumarray throws an error 'Error using accumarray: First input SUBS must contain positive integer subscripts.'.

Is there way where I can use accumarray for float subscripts like the one in this problem?

Any other alternate methods are also welcome.

Stephen Rauch
  • 1,783
  • 11
  • 22
  • 34
  • Since this question is purely about implementation, it is more on topic for stackoverflow and you would have gotten a faster answer there. – Dan Nov 21 '17 at 16:50
  • Thanks for the suggestion Dan! But there is a question ban which has been put there for my account and hence had to ask it here due to lack of options. – shripati007 Nov 22 '17 at 04:28

1 Answers1

0

You can use the third output of unique to convert your floats to a format suitable for accumarray:

[C,~,ib] = unique(num_dist(:,2))
num_dist_final(:,1) = accumarray(ib,num_dist(:,1))
num_dist_final(:,2) = C
Stephen Rauch
  • 1,783
  • 11
  • 22
  • 34
Dan
  • 161
  • 6