Resizing cells in a cell array
17 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Jos (10584)
2014-3-19
Let C be your cell array
D = C(~cellfun(@(x) size(x,1) < 50 && size(x,2) < 40,C))
0 个评论
更多回答(3 个)
Image Analyst
2014-3-19
编辑:Image Analyst
2014-3-19
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]
0 个评论
Dawid J
2014-3-19
2 个评论
Image Analyst
2014-3-20
编辑:Image Analyst
2014-3-20
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!