从大矩阵中提取特定数字的对应值

Extracting corresponding values of particular number from a large matrix

我有一个 2x5000 矩阵,其中包含刺激值(1 到 7)和相应的响应。刺激值随机散布在数据中。 喜欢:

2   0
3   1
3   1
1   1
7   0
6   0
7   1
6   1
1   0
1   0
7   1
6   1
4   1

我需要计算刺激的每个值的响应平均值(例如,我在这里需要 7 个平均值)。

但我不知道如何将不同变量中的值及其对应的值分开(例如如何提取6及其对应的数据)。

第二个相似矩阵在原始值上添加了噪声,这变得更加棘手,我希望从中提取 1 到 1.9,... 6 到 6.9 和相应的值。

我正在尝试 find(data_experiment == 4) (例如)并且它 returns 我的 2x5000 矩阵中所有 4 的位置,但我不知道如何提取相应的数据.

还有比'find'更好的吗?

谁能推荐一下可以使用哪些功能?

您可以使用 accumarray 函数来解决它:

这是一个示例代码:

D = [2   0
     3   1
     3   1
     1   1
     7   0
     6   0
     7   1
     6   1
     1   0
     1   0
     7   1
     6   1
     4   1];

%Compute sums: 
%Result: S(1) sumSecCol(where FirstCol = 1), S(2) = sumSecCol(where FirstCol = 2), S(3) = sumSecCol(where FirstCol = 3), 
S = accumarray(D(:,1), D(:,2));

%Compute count (replace D(:,2) with "1"s):
C = accumarray(D(:,1), ones(size(D,1), 1));

%Remove zeros from C - to avoid division by zero
C = max(C, 1);

%Compute mean
M = S ./ C;

结果:

M =

    0.3333
         0
    1.0000
    1.0000
         0
    0.6667
    0.6667

对于噪声情况“1 到 1.9,... 6 到 6.9”,您可以使用 floor 函数:

D = floor(D);

1 到 1.9999 转到 1,2 到 2.9999 转到 2...

看起来round是副标题,但是你描述的操作适用floor


这是一个使用 for 循环的解决方案,没有使用 accumarray

S = zeros(7, 1);
C = zeros(7, 1);

D1 = D(:, 1); %First column
D2 = D(:, 2); %Second column

for i = 1:7
    S(i) = sum(D2(D1 == i));
    C(i) = sum(D1 == i);
end

M = S ./ max(C, 1);

建议使用logical indexing,而不是使用find