Delete a cell in an array chosen by a criterion?

1 次查看(过去 30 天)
Dear all, I have a cell-array of 9*365. In each cell is a vector of 3*365. First I want to delete every 6th and 7th cell in each row. Then I want to delete them by a criterion in the vector. The third column in every vector stands for the trading volume of a stock. If all rows of this column are zero, then the cell should be deleted. How can I delete the cells? Thank you very much!
  7 个评论
Thor
Thor 2013-1-22
Dear all, I am sorry. I misdescribed it. I have a cell array of 9*365. The nine is for different years and the 365 is for every day in a year. First I want to delete every 6th and 7th cell in each year(for me this means in each row of the cell array). Because I want to delete the weekends. Then in every cell is a 270-288*3 matrix! Not a vector! The rows could differ because I don't have for every day, the same points of the intra-day stock prices. The third column of the matrix is the trading volume. If all values in this column are zero, then the whole matrix should be deleted. Thank you!

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-1-22
编辑:Azzi Abdelmalek 2013-1-22
% If your marices are 365x3
x(:,6:6:end)=[]; % x is your cell array
x(:,6:6:end)=[];
[n,m]=size(x);
for k=1:n
for l=1:m
v=x{k,l};
if ~any(v(:,3))
x{k,l}=[]
end
end
end

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2013-1-22
years = 2002:2010; % 9 years
ndy = 337 + eomday(years,2); % days in years
data = cell(1,numel(years)); % create your cell-array
for jj = 1:numel(data)
data{jj} = cell(ndy(jj),1);
c = randperm(ndy(jj));
b = c(1:randi(20));
for ii = 1:ndy(jj)
k = randi([270,288]);
data{jj}{ii} = randi([40,1000],k,2);
if any(ismember(b,ii))
data{jj}{ii}(:,3) = 0;
else
data{jj}{ii}(:,3) = randi([40,1000],k,1);
end
end
end
% solution
for jj = 1:numel(years)
data{jj} = data{jj}...
(~ismember(weekday(datenum(years(jj),1,(1:ndy(jj))')),[1,7]));
for ii = 1:numel(data{jj})
if all(~data{jj}{ii}(:,3))
data{jj}{ii} = [];
end
end
end

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by