I have a cell array. For example, The dimension of this cell is [2x3 , 3x3 , 2x4]. I want to find the maximum size of this array and make this cell array in equal size. I want to add zeros to the minimum cell size to make it as equal to maximum cell size.
I tried with this code:
sz = cellfun(@(x)size(x,2), A);
minLength = min(sz);
B = cell2mat(cellfun(@(x)x(1:minLength), A, 'uniformoutput', false));'
But its not working. It would be nice if anybody help me out.
DVarga
21.9k6 gold badges57 silver badges60 bronze badges
asked Jun 6, 2016 at 11:06
Md. Abdur Rahim
1291 gold badge2 silver badges10 bronze badges
1 Answer 1
This should work
A = {rand(2,3), rand(3,3), rand(2,4)}
% Find the maximum numbers rows (dims(1)) and the maximum number of columns (dims(2))
dims = max(cell2mat(cellfun(@(x)size(x), A, 'uni', 0)'));
%Pad each element of A with zeros so that it's size becomes dims
B = cellfun(@(x)[x,zeros(size(x,1),dims(2)-size(x,2));zeros(dims(1)-size(x,1),dims(2))], A,'uni', 0)
This results in
B =
[3x4 double] [3x4 double] [3x4 double]
And more specifically:
>> B{1}
ans =
0.9028 0.5791 0.0366 0
0.5763 0.0658 0.3373 0
0 0 0 0
>> B{2}
ans =
0.0764 0.1326 0.4413 0
0.2463 0.0238 0.3726 0
0.0299 0.2610 0.1408 0
>> B{3}
ans =
0.6266 0.6141 0.5653 0.5951
0.3176 0.0741 0.5795 0.2600
0 0 0 0
answered Jun 6, 2016 at 11:55
Dan
45.8k20 gold badges98 silver badges169 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-matlab
{[2x3], [3x3], [2x4]}case? I'm not really understanding what the goal is.