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.
채택된 답변
추가 답변 (3개)
%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
2019년 3월 7일
Zara Khan
2019년 3월 8일
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
2019년 3월 8일
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
2019년 3월 17일
madhan ravi
2019년 3월 18일
M=reshape(cellfun('prodofsize',tst),1,[]);
maxlen=max(M);
Zara Khan
2019년 3월 18일
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
2019년 3월 18일
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?
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)
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
Zara Khan
2019년 3월 7일
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
2019년 3월 7일
Zara Khan
2019년 3월 8일
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
2019년 3월 18일
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
Zara Khan
2019년 3월 18일
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
2019년 3월 18일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!