Resizing cells in a cell array

7 views (last 30 days)
Dawid J
Dawid J on 19 Mar 2014
Edited: Dawid J on 21 Mar 2014
Hi guys,
I have a problem concerning cells. Lets say I have a cell array which has 9 cells within, it looks like that:
znaki =
Columns 1 through 7
[50x226 logical] [15x226 logical] [40x28 logical] [40x27 logical] [39x22 logical] [40x22 logical] [40x21 logical]
Columns 8 through 9
[40x22 logical] [40x24 logical]
What I'd like to know is how to delete the cells which dimensions are:
- rows should be less than 50 - columns should be less than 40.
So then it should delete these cells: [50x226 logical] [15x226 logical].
Thanks for help!

Accepted Answer

Jos (10584)
Jos (10584) on 19 Mar 2014
Let C be your cell array
D = C(~cellfun(@(x) size(x,1) < 50 && size(x,2) < 40,C))

More Answers (3)

Image Analyst
Image Analyst on 19 Mar 2014
Edited: Image Analyst on 19 Mar 2014
You could use an easy, simple, intuitive for loop:
znaki ={false(50,226), false(15,226), false(40,28), false(40,27), false(39,22), false(40,22), false(40,21), false(40,22), false(40,24)}
for c = length(znaki) : -1 : 1
[rows, columns] = size(znaki{c});
fprintf('Cell #%d has %d rows and %d columns\n', c, rows, columns);
if rows > 50 || columns > 40
znaki(c) = [];
fprintf('Deleted cell #%d.\n', c);
end
end
znaki % Show in command window.
In command window:
znaki =
Columns 1 through 8
[50x226 logical] [15x226 logical] [40x28 logical] [40x27 logical] [39x22 logical] [40x22 logical] [40x21 logical] [40x22 logical]
Column 9
[40x24 logical]
Cell #9 has 40 rows and 24 columns
Cell #8 has 40 rows and 22 columns
Cell #7 has 40 rows and 21 columns
Cell #6 has 40 rows and 22 columns
Cell #5 has 39 rows and 22 columns
Cell #4 has 40 rows and 27 columns
Cell #3 has 40 rows and 28 columns
Cell #2 has 15 rows and 226 columns
Deleted cell #2.
Cell #1 has 50 rows and 226 columns
Deleted cell #1.
znaki =
[40x28 logical] [40x27 logical] [39x22 logical] [40x22 logical] [40x21 logical] [40x22 logical] [40x24 logical]

Dawid J
Dawid J on 19 Mar 2014
Thanks ~ Image Analyst. It looks nice, but unfortunately the cells in array aren't always the same dimension so I would have to change it manually.
The solution posted by ~Jos (10584) looks very good and I get what I wanted. Thanks!
And the last question I'd like to ask:
Is there any way to make all of the cells in array the same dimension? If I have now :
znaki2 =
[37x26 logical] [36x25 logical] [36x20 logical] [36x12 logical] [36x20 logical] [37x20 logical]
all of them consist of 0 and 1, and I want to make them for example 42x30. So can I change the dimensions by somehow filling it up with "fake" zeros?
  2 Comments
Image Analyst
Image Analyst on 20 Mar 2014
Edited: Image Analyst on 20 Mar 2014
I really thought you would have chosen mine, because it gives you what you asked for: it deletes the rows. Jos's code does not - it returns what you wanted to get rid of. If you run his code:
znaki ={false(50,226), false(15,226), false(40,28), false(40,27), false(39,22), false(40,22), false(40,21), false(40,22), false(40,24)}
D = znaki(~cellfun(@(x) size(x,1) < 50 && size(x,2) < 40,znaki))
you can see that it gives:
D =
[50x226 logical] [15x226 logical]
which is actually the exact opposite of what you asked for . It did not return the array with the "bad" cells deleted. Mine did. It returned the cells you wanted deleted, not the cells you wanted to keep. But maybe you misstated what you want or something.
And I don't know what you mean by "unfortunately the cells in array aren't always the same dimension so I would have to change it manually." Neither my not Jos's code requires cells to be the same dimension, nor delivers output in the same dimension. They're both general enough to handle arrays in the cells of any dimensions. Plus, you'll note that you never said anything about that in your original question.
If you do want the outputs to be all the same dimension, simply use (untested)
cellContents = znaki{cellNumber}; % Extract array from this cell.
cellContents(42,30) = 0; % Pad down and to the left with 0's.
znaki{cellNumber} = cellContents; % Stuff back in.
You might be able to do this all in one line if you want a more compact form.
Dawid J
Dawid J on 21 Mar 2014
Edited: Dawid J on 21 Mar 2014
Hey ~Image Analyst. I know that Jos's code gave me opposite result but I just simply changed the "&&" to "||" and it works.
I appreciate your effort - it was really instructive and thanks for you help!

Sign in to comment.


Dawid J
Dawid J on 20 Mar 2014
bump

Community Treasure Hunt

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

Start Hunting!