Deleting empty variables from cell variables with respect to rows.

1 view (last 30 days)
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 Comment
RDG
RDG on 11 Oct 2013
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

Sign in to comment.

Accepted Answer

Matt J
Matt J on 11 Oct 2013
Edited: Matt J on 11 Oct 2013
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!