基于标准在数组/向量中添加单元格 - Matlab

时间:2014-04-11 01:49:16

标签: arrays matlab sorting add

我有一个数据向量448X2,其中第一列对应距离(从-50到224),第二列包含与每个距离关联的强度。

我需要添加所有与距离无关的强度,无论方向如何(正/负)。例如,我想添加1的间隔。因此,如果在[-1 1]距离范围内有5个值,我需要添加它们的所有强度。然后,从[-2 -1]& [1 2]距离等等......

我开始将数据从0缩短为224

datasort = sortrows(abs(data));

带有负数的蛋糕。现在我的数据按距离递增排序,我不知道如何在距离间隔内添加强度。

这是我的datasort向量的前10行:

0.119865417480469   0.0115106217563152
0.669918060302734   0.356697350740433
0.909893035888672   0.659623980522156
1.45951080322266    1.69302666187286
1.70020294189453    2.30387997627258
2.24539566040039    3.98834538459778
2.49082183837891    4.94714736938477
3.02403259277344    7.16749382019043
3.28177261352539    8.59203147888184
3.79549789428711    11.1872768402100

我想结束这样的事情:

1   1.02780000000000
2   3.99690000000000
3   8.93550000000000
4   26.9468000000000

其中第一列现在包含1-224(1:224)的离散距离值,第二列包含距离范围的所有强度之和。

我希望我足够清楚。感谢

更新:

我想我能做到:

datasort(:,1) = round(datasort(:,1));

这给了我(前10分),

0   0.0115106217563152
1   0.356697350740433
1   0.659623980522156
1   1.69302666187286
2   2.30387997627258
2   3.98834538459778
2   4.94714736938477
3   7.16749382019043
3   8.59203147888184
4   11.1872768402100

我仍然不知道如何以相同的距离添加强度!!!

4 个答案:

答案 0 :(得分:0)

这是一种方式:

datasort = sortrows(abs(data));
datasort(:,1) = 1+floor(datasort(:,1));
inSum=zeros(1,max(datasort(:,1)));
for i=1:max(datasort(:,1))
    inSum(i)=sum(datasort(i==datasort(:,1),2));
end

答案 1 :(得分:0)

我首先使用histc来存储数据的第一列,然后将histc返回的索引传递给在第二列上运行的accumarray。请注意,您甚至不需要对数据进行排序以执行此操作,但您仍需要致电abs以删除否定数字。

>> data = [0.119865417480469   0.0115106217563152
0.669918060302734   0.356697350740433
0.909893035888672   0.659623980522156
1.45951080322266    1.69302666187286
1.70020294189453    2.30387997627258
2.24539566040039    3.98834538459778
2.49082183837891    4.94714736938477
3.02403259277344    7.16749382019043
3.28177261352539    8.59203147888184
3.79549789428711    11.1872768402100]

data =

         0.119865417480469        0.0115106217563152
         0.669918060302734         0.356697350740433
         0.909893035888672         0.659623980522156
          1.45951080322266          1.69302666187286
          1.70020294189453          2.30387997627258
          2.24539566040039          3.98834538459778
          2.49082183837891          4.94714736938477
          3.02403259277344          7.16749382019043
          3.28177261352539          8.59203147888184
          3.79549789428711            11.18727684021

>> [~, idx] = histc(data(:,1), [0:224]) 

idx =

     1
     1
     1
     2
     2
     3
     3
     4
     4
     4

>> result = accumarray(idx, data(:,2))

result =

           1.0278319530189
          3.99690663814544
          8.93549275398255
          26.9468021392823

>> 

希望有所帮助!

编辑:

只是为了确认这确实有效,而不对数据进行排序

>> % Unsort your data snippet to illustrate
>> data = data([randperm(length(data))],:)

data =

          3.79549789428711            11.18727684021
          1.45951080322266          1.69302666187286
         0.909893035888672         0.659623980522156
          2.24539566040039          3.98834538459778
          1.70020294189453          2.30387997627258
         0.119865417480469        0.0115106217563152
         0.669918060302734         0.356697350740433
          3.28177261352539          8.59203147888184
          3.02403259277344          7.16749382019043
          2.49082183837891          4.94714736938477

>> [~, idx] = histc(data(:,1), [0:224])

idx =

     4
     2
     1
     3
     2
     1
     1
     4
     4
     3

>> result = accumarray(idx, data(:,2))

result =

           1.0278319530189
          3.99690663814544
          8.93549275398255
          26.9468021392823

>>

答案 2 :(得分:0)

您可以使用逻辑索引从矩阵中提取这些行,然后总结第2列。

m = floor(min(x(:,1)));
M = floor(max(x(:,1)));
for i = m:M 
   sum(x(x(:,1)=>i & x(:,1)<=i+1, 2));
end

答案 3 :(得分:0)

accumarray可以在一行中执行此操作:

result = accumarray(ceil(datasort(:,1)), datasort(:,2));