How to add zeros to the end of cells

조회 수: 5 (최근 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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by