【问题标题】:converting 1 matrix into multiple matrices将 1 个矩阵转换为多个矩阵
【发布时间】:2013-10-30 05:19:22
【问题描述】:

我有一个相当大的矩阵,其中第 1 列以 100 秒为单位列出时间(例如 3000 = 30 秒),第 2 列是事件代码(例如 1 = 试验开始,2 = 响应开始,4 = 结束审判)。我已经使用这个更大的矩阵来确定各种索引。但是,现在我需要逐个尝试计算一个索引(即基于 1 到 4 之间的值,总共重复 60 次。下面是一个由 3 个试验组成的示例数组:

0       1
682     2
987     3
2586    2
2593    3
2598    2
2601    3
2602    2
2605    3
2607    2
2608    3
2635    2
2636    3
5546    4
7321    1
7826    2
7900    3
7901    2
7902    3
9481    2
9730    3
9877    2
10319   3
10431   4
11158   1
11361   2
11376   3
12209   2
12267   3
13547   2
14159   4

我想做的是为每对 1 和 4 对填充一个新数组,例如,在这个 3 x 1 和 4 对数组中,第一个试验将包含以下内容

Tarray_1 = [0,682,987,2586,2593,2598, 2601, 2602, 2605, 2607, 2608,2635, 2636, 5546; 1,2,3,2,3,2,3,2,3,2,3,2,3,4]; 

是否有任何简单或直接的方法可以做到这一点?在过去的几天里,我一直在尝试使用 for 循环和 find 命令来确定每个 1 的索引,以便创建新数组。有人对我能做什么有任何建议吗?

【问题讨论】:

    标签: matlab matrix


    【解决方案1】:

    给定一个n-by-2 矩阵M,这是一个简单的方法,只需两个命令:

    >> trialStartEnd = [find(M(:,2)==1) find(M(:,2)==4)]
    
    trialStartEnd =
    
         1    14
        15    24
        25    31
    
    >> T = arrayfun(@(x,y) M(x:y,:)',trialStartEnd(:,1),trialStartEnd(:,2),'uni',0)
    
    T = 
    
        [2x14 double]
        [2x10 double]
        [2x7  double]
    

    每个试验在T 的单元格中都有自己的数组。

    【讨论】:

      【解决方案2】:

      如果我理解正确的话,这是一种方法:

      A = [0       1;
      682     2;
      987     3;
      2586    2;
      2593    3;
      2598    2;
      2601    3;
      2602    2;
      2605    3;
      2607    2;
      2608    3;
      2635    2;
      2636    3;
      5546    4;
      7321    1;
      7826    2;
      7900    3;
      7901    2;
      7902    3;
      9481    2;
      9730    3;
      9877    2;
      10319   3;
      10431   4;
      11158   1;
      11361   2;
      11376   3;
      12209   2;
      12267   3;
      13547   2;
      14159   4];
      
      
      
      
      % all even codes
      evenCodes = A(:, 2);
      
      
      % find positions of valus 4 in the even cods
      [indx c] = find(evenCodes == 4);
      
      
      % just append 0 to the idx array
      newIdxOfFours = [0, indx'];
      
      
      % cell cointaining Tarrays, e.g. outCell{1} is Tarray_1
      TarrayCell = {};
      
      for i = 1:length(newIdxOfFours) - 1
      
          % find starting and ending index/row for each trial
          startIdx = newIdxOfFours(i) + 1;
          endIdx = newIdxOfFours(i+1);
      
          TarrayCell{end + 1} = A(startIdx:endIdx, :)';
      
      
      end
      
      TarrayCell{1}
      TarrayCell{2}
      TarrayCell{3}
      
      >> 
      ans =
      
                 0         682         987        2586        2593        2598        2601        2602        2605        2607        2608        2635        2636        5546
                 1           2           3           2           3           2           3           2           3           2           3           2           3           4
      
      
      ans =
      
              7321        7826        7900        7901        7902        9481        9730        9877       10319       10431
                 1           2           3           2           3           2           3           2           3           4
      
      
      ans =
      
             11158       11361       11376       12209       12267       13547       14159
                 1           2           3           2           3           2           4
      

      【讨论】:

        【解决方案3】:

        我编写了这段代码,它可能适用于您的代码,但是根据您需要这些矩阵做什么,将它们放在单元格中可能会很麻烦。但是在没有其他答案的情况下,这应该可以解决问题。我复制了你给的大矩阵并命名为A

        [F,I]=find(A(:,2)==4);
        T=cell(1,sum(I));
        T{1}=A(1:F(1),:);
        for j=2:sum(I)
            T{j}=A(F(j-1)+1:F(j),:);
        end
        

        这会生成一个矩阵元胞数组T,您可以使用例如T{1} 获取任何矩阵,或者使用例如T{1}(2,1) 访问特定元素。

        编辑:这假设矩阵以 1 开头,并且另一个 1 总是跟在 4 之后。Chappjc 的答案可能更好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多