How can I make vectors within cell array consistent in length?

조회 수: 3 (최근 30일)
Farshid Daryabor
Farshid Daryabor 2020년 3월 10일
댓글: Farshid Daryabor 2020년 3월 10일
Please find attached file I want to make the all arrays in same length. I tried the comment below, doesn't work. I really appreciate in advance.
>> N = cellfun(@numel, X);
>> M = max(N);
>> OUT = cellfun(@(a) [a; nan(M - numel(a),1)],X,'uni',0);
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in @(a)[a;nan(M-numel(a),1)]
  댓글 수: 1
Farshid Daryabor
Farshid Daryabor 2020년 3월 10일
I did it, just for the other user it can be usful.
[X_out, tf_x] = cellfun(@(x) padcat(x{:}), X, 'uni' , 0); % pads with NaNs
n = max(cellfun('size',X_out,1));
m = max(cellfun('size',X_out,2));
X_new = cell2mat(cellfun(@(x) [x, nan(size(x, 1), m-size(x, 2)); ...
nan(n-size(x, 1), m)], X, 'uni' , 0));

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by