フィルターのクリア

How to add zeros to the end of cells

2 ビュー (過去 30 日間)
Lexington Stoyell
Lexington Stoyell 2018 年 3 月 5 日
コメント済み: Walter Roberson 2018 年 3 月 6 日
I am looping through data, which are of various lengths. I want to create a cell that will attach zeros to the end of the cells so there is not a dimension mismatch. How do I go about doing this? How do I do this if the first set of data to loop through is not the largest length?
  7 件のコメント
Geoff Hayes
Geoff Hayes 2018 年 3 月 5 日
@Jan - I didn't realize you could do that!
Lexington Stoyell
Lexington Stoyell 2018 年 3 月 5 日
How do you then add the zeros on the end?

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

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 3 月 5 日
FirstNCols = @(M,n) M(:,1:n);
PadToN = @(M,n) FirstNCols([M, zeros(size(M,1),n)]);
width_needed = max( cellfun(@(M) size(M,2), YourArray) );
PaddedArray = cellfun(@(M) PadToN(M, width_needed), YourArray, 'uniform', 0);
  4 件のコメント
Lexington Stoyell
Lexington Stoyell 2018 年 3 月 6 日
When implementing this I get the error as follows:
Error using @(M,n)M(:,1:n)
Not enough input arguments.
Error in @(M,n)FirstNCols([M,zeros(size(M,1),n)])
Error in @(M)PadToN(M,width_needed)
Error in limits (line 34)
PaddedArray = cellfun(@(M) PadToN(M, width_needed), YourArray, 'uniform', 0);
Walter Roberson
Walter Roberson 2018 年 3 月 6 日
FirstNCols = @(M,n) M(:,1:n);
PadToN = @(M,n) FirstNCols([M, zeros(size(M,1),n)], n);
width_needed = max( cellfun(@(M) size(M,2), YourArray) );
PaddedArray = cellfun(@(M) PadToN(M, width_needed), YourArray, 'uniform', 0);

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by