从嵌套单元格中提取(价格数据为)一维向量
Extract (price data as) 1D vector from nested cell
鉴于:
data = { ...
{ '2014-12-31' [79.2100] } ...
{ '2014-12-30' [79.3000] } ...
{ '2014-12-26' [79.1200] } };
我需要提取:
prices = [ 79.2100, 79.3000, 79.1200 ];
完成此任务的好方法是什么? for
循环实现起来很简单,并且结合预分配会相当有效。但是看起来有点笨拙。
您可以先将元胞数组转换为 而不是 嵌套数组,然后提取第二列并使用 [=11= 从元胞数组转换为普通数组] 结合 {}
索引。
% Turn your nested cell array into an N x 2 cell array
data = cat(1, data{:});
% Get just the second column
prices = cat(2, data{:,2});
鉴于:
data = { ...
{ '2014-12-31' [79.2100] } ...
{ '2014-12-30' [79.3000] } ...
{ '2014-12-26' [79.1200] } };
我需要提取:
prices = [ 79.2100, 79.3000, 79.1200 ];
完成此任务的好方法是什么? for
循环实现起来很简单,并且结合预分配会相当有效。但是看起来有点笨拙。
您可以先将元胞数组转换为 而不是 嵌套数组,然后提取第二列并使用 [=11= 从元胞数组转换为普通数组] 结合 {}
索引。
% Turn your nested cell array into an N x 2 cell array
data = cat(1, data{:});
% Get just the second column
prices = cat(2, data{:,2});