Deleting empty variables from cell variables with respect to rows.

2 次查看(过去 30 天)
Suppose, I have this code excerpt:
clc, clear, close all;
A{1}=[1,4,1,6,130,30,4;1,6,3,6,130,30,4;1,6,2,1,130,30,4;1,1,2,2,130,200,4;1,4,1,1,130,30,4;
1,2,2,2,130,100,4;2,4,1,4,75,30,4;2,6,1,4,75,30,4;2,4,5,2,75,30,4;2,2,3,2,75,100,4;
2,5,4,6,75,20,4;2,6,5,5,75,30,4;4,1,5,6,117,200,3;4,2,3,5,117,100,3;4,3,2,4,117,9,3;
4,2,3,1,117,100,3;4,5,3,1,117,20,3;4,1,2,6,117,200,3;4,2,2,3,117,100,3]
A{2}=[1,1,1,6,130,200,4;1,3,3,4,130,9,4;1,2,3,3,130,100,4;1,4,3,5,130,30,4;1,3,1,2,130,9,4;
1,1,3,4,130,200,4;2,6,3,5,75,30,4;2,6,3,6,75,30,4;2,1,3,2,75,200,4;2,6,1,5,75,30,4;
2,5,1,2,75,20,4;2,5,2,1,75,20,4;4,6,2,6,117,30,3;4,2,2,3,117,100,3;4,3,3,3,117,9,3;
4,6,5,2,117,30,3;4,6,2,3,117,30,3;4,6,3,3,117,30,3;4,3,5,2,117,9,3]
for i=1:length(A)
for j=1:19
filtered{i,j}=A{i}(ismember(A{i}(:,1),[j]),:);
end
end
The filtered content consists of 2x19 cells. I would like to remove the empty variables (The ones with []) from filtered. I tried this code,
A=filtered(~cellfun(@isempty,filtered))
But it merges the 2 rows into 1 column. I would like to maintain the existing row position. In other words, it should remove the empty cell variables based on its row.
How can I get about this?
  1 个评论
RDG
RDG 2013-10-11
Did a bit of tweaking. This may not be the best solution.
for i=1:length(A)
for j=1:19
filtered{j}=A{i}(ismember(A{i}(:,1),[j]),:);
end
newfiltered{i}=filtered(~cellfun(@isempty,filtered(:)));
end

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2013-10-11
编辑:Matt J 2013-10-11
You can still do this
A=filtered(~cellfun('isempty',filtered)),
but reshape it afterward
A=reshape(A,2,[]);
assuming it will always be reshapeable into something 2xN.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by