如何使用cellfun,matlab从单元格数组中删除行的范围

问题描述 投票:0回答:1

我有一个1x3的单元格,带有

timepositionrawdata = {time, lat, lon};
% Extract every fourth instance of the raw data
time = timepositionrawdata{1,1}(1:4:end);
lat = timepositionrawdata{1,2}(1:4:end);
lon = timepositionrawdata{1,3}(1:4:end);
timepositionrawdata = {time, lat, lon};

% Delete and add appropriate rows to match image sequence times
timepositionrawdata{1}(278,:) = [];
timepositionrawdata{2}(278,:) = [];
timepositionrawdata{3}(278,:) = [];

timepositionrawdata = timepositionrawdata{1}([1:281, 281:end],:);
timepositionrawdata = timepositionrawdata{2}([1:281, 281:end],:);
timepositionrawdata = timepositionrawdata{3}([1:281, 281:end],:);

我希望能够删除整个单元格中的特定行。您如何使用cellfun来做到这一点?

matlab cell delete-row
1个回答
0
投票

您首先需要编写一个可以正确处理一个单元格的函数,然后使用cellfun将其应用于单元格数组。

function time = process_one_cell(time)
time = time(1:4:end);
time(278, :) = [];
time = time([1:281, 281:end], :); % I'm not sure why you're doing this, looks kind of pointless. 
end

然后按如下所示调用函数

timepositionrawdata = cellfun(@process_one_cell, timepositionrawdata, 'UniformOutput', 0);
% or if you prefer to be more verbose
timepositionrawdata = cellfun(@(x) process_one_cell(x), timepositionrawdata, 'UniformOutput', 0);
© www.soinside.com 2019 - 2024. All rights reserved.