How to pad empty cells with the last number in the row?

2 ビュー (過去 30 日間)
maruljay
maruljay 2019 年 10 月 17 日
コメント済み: maruljay 2019 年 10 月 17 日
I want to convert a cell array (WS) to matrix. In order to do that I need to have the same number of rows in all arrays. To do that I padded the empty cells with zeros as shown below:
maxLengthCell=max(cellfun('size',WS,2));
for i=1:length(WS)
for j=cellfun('size',WS(i),2)+1:maxLengthCell
WS{i}(j)=0;
end
end
A=cell2mat(WS);
Instead of padding it with zeros, I want to fill the empty cells in each array with the last number in the row (ex: 1 5 8 6 4 9 9 9 9 9 9 9 ).
How can I do this?

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 17 日
FirstN = @(v,N) v(1:N);
PadLastN = @(v,N) FirstN([v, repmat(v(end),1,N)],N);
WS = cellfun( @(C) PadLastN(C, maxLengthCell), WS, 'uniform', 0);
  1 件のコメント
maruljay
maruljay 2019 年 10 月 17 日
Thank you. Worked like a charm!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by