How can I make each cell array consistent in length?

In a cell array each element is 1Xlength. This length is not equal. I want to get the maximum length and then want to make each cell with same length by padding zeros in it.

 採用された回答

Jos (10584)
Jos (10584) 2019 年 3 月 18 日

4 投票

A final attempt to answer this question :-)
C = {1:4 1:2 ; 1:5 1:6 ; 1 1:3} % a m-by-n cell array
N = cellfun(@numel, C) % old lengths of cell elements
M = 3 ; % new length should be multiple of M
newN = M * ceil(N / M) % new lengths of cell elements
padfun = @(k) [C{k} zeros(1, newN(k) - N(k))] ;
C2 = arrayfun(padfun, 1:numel(C) , 'un', 0) ; % apply padding to all elements of C
C2 = reshape(C2, size(C)) % reshape (if needed)

5 件のコメント

Zara Khan
Zara Khan 2019 年 3 月 18 日
Jos (10584):
Thank you so much. You are really great. You dont know how you have saved me. I was trying this from long . Thanks again.
But I apologise for mixing up two questions here. This was my another question where I asked how to divide a cell by32.
Jos (10584)
Jos (10584) 2019 年 3 月 18 日
You're welcome, and thank you. I am glad we sorted it out :-)
Zara Khan
Zara Khan 2019 年 3 月 19 日
I am asking something more:
How can I put a condition here to check whether cell length is less than M or not . If it is less than M then only there will be padding otherwise not ?
Jos (10584)
Jos (10584) 2019 年 3 月 19 日
After you calculated the new N you can use this:
tf = ~(N < M) % true for the large cells
newN(tf) = N(tf) % reset to the old lengths
Zara Khan
Zara Khan 2019 年 3 月 19 日
Thank you . It works.

サインインしてコメントする。

その他の回答 (3 件)

tmarske
tmarske 2019 年 3 月 7 日
編集済み: tmarske 2019 年 3 月 7 日

1 投票

%set up a dummy example
tst = {[1 1], [1 1 1 1 1], [1 1 1], [1]}
%get the maximum length
maxlen = max(cellfun(@length, tst))
%pad zeros
tstPadded = cellfun(@(x)([x zeros(1, maxlen - length(x))]), tst, 'UniformOutput', false)

13 件のコメント

madhan ravi
madhan ravi 2019 年 3 月 7 日
Zara Khan
Zara Khan 2019 年 3 月 8 日
tmarske:
I am working with mXn cell array. so I am getting this error "Size inputs must be scalar" while implementing your code.
madhan ravi
madhan ravi 2019 年 3 月 8 日
編集済み: madhan ravi 2019 年 3 月 8 日
maxlen = max(cellfun('prodofsize', tst),[],[1 2]); % tst is m x n cell array
tstPadded = cellfun(@(x)([x zeros(1, maxlen - numel(x))]), tst, 'un', 0)
Zara Khan
Zara Khan 2019 年 3 月 8 日
Now I am getting this : Dimension argument must be a positive integer scalar within indexing range.
madhan ravi
madhan ravi 2019 年 3 月 8 日
編集済み: madhan ravi 2019 年 3 月 8 日
>> tst = {[1 3 2] 7 6 ; 1:6 [11 2] 6}
maxlen = max(cellfun('prodofsize', tst),[],[1 2]); % tst is m x n cell array
tstPadded = cellfun(@(x)([x zeros(1, maxlen - numel(x))]), tst, 'un', 0)
tst =
2×3 cell array
{1×3 double} {[ 7]} {[6]}
{1×6 double} {1×2 double} {[6]}
tstPadded =
2×3 cell array
{1×6 double} {1×6 double} {1×6 double}
{1×6 double} {1×6 double} {1×6 double}
>> tstPadded{:}
ans =
1 3 2 0 0 0
ans =
1 2 3 4 5 6
ans =
7 0 0 0 0 0
ans =
11 2 0 0 0 0
ans =
6 0 0 0 0 0
ans =
6 0 0 0 0 0
>>
Zara Khan
Zara Khan 2019 年 3 月 17 日
madhan ravi :
maxlen = max(cellfun('prodofsize', tst),[],[1 2]);
Error using max
Dimension argument must be a positive integer scalar within indexing range.
madhan ravi
madhan ravi 2019 年 3 月 18 日
M=reshape(cellfun('prodofsize',tst),1,[]);
maxlen=max(M);
Zara Khan
Zara Khan 2019 年 3 月 18 日
Its easy. Thanks for the response. But I am always facing the same problem when using cellfun
Error using cellfun
Input #2 expected to be a cell array, was double instead.
Error in prog_32 (line 66)
tstPadded = cellfun(@(x)([x zeros(1, maxlen - numel(x))]), M, 'un', 0)
Each cell consists of consequtives 0's and 1's. Whenever I am doing padding I am getting this error.
madhan ravi
madhan ravi 2019 年 3 月 18 日
編集済み: madhan ravi 2019 年 3 月 18 日
I have no problem with the code , I have no idea why you get an error upload the code & data(as .mat file) that your trying.
Zara Khan
Zara Khan 2019 年 3 月 18 日
A=cell(numImages,8);
B{k,j}=profile;
A=B;
len = cellfun(@length, B);
len1 = 32 * ceil(len/32);
width_needed=cell(numImages,8);
width_needed=len1-len;
I am making each cell a multiple of 32. Now my task is to pad extra zero after the multiplication. And I want to keep them as cell format , no merging will be done. Here the profile is varying in length. Basically it consists of consequtives 0's and 1's. I am attaching few pictures here too.
Jos (10584)
Jos (10584) 2019 年 3 月 18 日
Your code makes little sense. You overwrite A and width_needed.
I thought you needed the maximum of the lengths, so why don't you apply max?
Zara Khan
Zara Khan 2019 年 3 月 18 日
編集済み: Zara Khan 2019 年 3 月 18 日
I dont want maxlength . I am making each cell a multiple of 32 . so i just want to add the extra places by zeros. maxlength will only find out maximum length among all.
Jos (10584)
Jos (10584) 2019 年 3 月 18 日
Your question reads otherwise ... " I want to get the maximum length and then want to make each cell with same length by padding zeros in it.

サインインしてコメントする。

Jos (10584)
Jos (10584) 2019 年 3 月 7 日
編集済み: Jos (10584) 2019 年 3 月 8 日

1 投票

If you make them the same length, you can also store them in a matrix. In that case, my PADCAT function is your friend :-)
C = {[1 2] ; 1 ; [1 2 3]}
[M, tf] = padcat(C{:}) % pads with NaNs
M(~tf) = 0
PADCAT can be found on the File Exchange, for free:
(edited answer)

6 件のコメント

Zara Khan
Zara Khan 2019 年 3 月 7 日
what it will be if there is C{i,j} like thing ?
Jos (10584)
Jos (10584) 2019 年 3 月 7 日
You mean a M-by-N cell array? I suggest you try it! :-)
C = {[1 2] 1 ; [1 2 3] [1 2]} % a 2-by-2 cell array with row vectors
Zara Khan
Zara Khan 2019 年 3 月 7 日
yes yes M×N cell array .
Zara Khan
Zara Khan 2019 年 3 月 8 日
This is shwoing wrong syntax : (M,tf) = padcat(C{:})
Jos (10584)
Jos (10584) 2019 年 3 月 8 日
My mistake, should be square brackets of course ... (I edited my answer).
(assuming you also downloaded and installed the function)
Zara Khan
Zara Khan 2019 年 3 月 18 日
Jos (10584):
by using padcat I am getting a matrix where all coloumns are merged. That I dont want. I want to keep each cell as it is, just want want to add extra zeros. Like , first cell is 1X16, second is 1X31 and so on. I want to work on each indivisually. By adding extra zeros the first cell will be suppose 1X32 and so on. Remember I am dealing with mXn cell array.

サインインしてコメントする。

Jos (10584)
Jos (10584) 2019 年 3 月 18 日

1 投票

C = {1:3 4 ; 5:9 10:12 ; 13:14 15} % a m-by-n cell array
N = cellfun(@numel, C)
maxN = max(N(:))
padfun = @(v) [v zeros(1, maxN - numel(v))] ;
C2 = cellfun(padfun, C , 'un', 0)

3 件のコメント

Zara Khan
Zara Khan 2019 年 3 月 18 日
Each cell array consists of consequtives 0's and 1's. Padding with extra zero showing me error
size input must be scalar.
Jos (10584)
Jos (10584) 2019 年 3 月 18 日
I do not get this error in the above code for a cell array like this
C = {[1 0 0 1], [0 1] ; [1 0 1 0], [0 0 1]}
You should give more details about the error and the input ...
Zara Khan
Zara Khan 2019 年 3 月 18 日
A=cell(numImages,8);
B{k,j}=profile;
A=B;
len = cellfun(@length, B);
len1 = 32 * ceil(len/32);
width_needed=cell(numImages,8);
width_needed=len1-len;
I am making each cell a multiple of 32. Now my task is to pad extra zero after the multiplication. And I want to keep them as cell format , no merging will be done. Here the profile is varying in length. Basically it consists of consequtives 0's and 1's. I am attaching few pictures here too.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2019 年 3 月 7 日

コメント済み:

2019 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by