基于另一个起始和结束指数矩阵的指数矩阵

时间:2016-04-07 09:56:40

标签: arrays matlab indexing

考虑一维数组:y(1:20)和矩阵形式的索引集合:indx = [1,3; 7,12; 16,19]

是否有一种巧妙的方法来获得一个单元格阵列:{y(1:3),y(7:12),y(16:19)}?

使用循环很容易完成,但我很想知道一种简单而清晰的方法将索引集合传递给1D数组。

1 个答案:

答案 0 :(得分:1)

我不认为有一种方法可以使用循环。您可以使用arrayfun作为简写:

arrayfun(@(from,to) y(from:to), indx(:,1), indx(:,2), 'uni', 0)

在您的数据上运行此结果

y = (1:20)*10;
indx = [1,3;7,12;16,19];

celldisp(arrayfun(@(from,to) y(from:to), indx(:,1), indx(:,2), 'uni', 0))

ans{1} =

    10    20    30



ans{2} =

    70    80    90   100   110   120



ans{3} =

   160   170   180   190
相关问题