0

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
4
  • What's the dimension of the cell again? I can understand [3, 3, 4] or [2 3 2] but not [2x3, 3x3, 2x4] Commented Jun 6, 2016 at 11:08
  • In my cell array , there are three cell element and each cell element has different size. Thats why I wrote {2x3, 3x3, 2x4}. @OfekShilon Commented Jun 6, 2016 at 11:15
  • 1
    Can you please provide the desired output for the {[2x3], [3x3], [2x4]} case? I'm not really understanding what the goal is. Commented Jun 6, 2016 at 11:48
  • I really can't understand what you want. Do you want a row-matrix as an output (filled with zeros)? And if yes, in what dimensions? Or? Commented Jun 6, 2016 at 11:54

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.