accumarray not working on float subscripts

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.
Thanks in advance.

 採用された回答

Stephen23
Stephen23 2017 年 11 月 21 日
編集済み: Stephen23 2017 年 11 月 21 日

0 投票

mat = [
35046, 0.17
1963, 0.34
1135, 0.51
405, 0.68
99, 0.85
1, 0.85
15, 1.02
2, 1.02];
% set tolerance value:
tol = 0.01;
[vec,~,idx] = unique(round(mat(:,2)/tol));
out = [accumarray(idx,mat(:,1)),vec*tol]
giving:
out =
35046 0.17
1963 0.34
1135 0.51
405 0.68
100 0.85
17 1.02

1 件のコメント

shripati pai
shripati pai 2017 年 11 月 22 日
Thanks Stephen! It worked in my case.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2017 年 11 月 21 日

編集済み:

2017 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by