How can I make each cell array consistent in length?

조회 수: 16 (최근 30일)
Zara Khan
Zara Khan 2019년 3월 7일
댓글: Zara Khan 2019년 3월 19일
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일
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
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일
%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
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일
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
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일
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by