How to add zeros to the end of cells

7 Ansichten (letzte 30 Tage)
Lexington Stoyell
Lexington Stoyell am 5 Mär. 2018
Kommentiert: Walter Roberson am 6 Mär. 2018
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 Kommentare
Geoff Hayes
Geoff Hayes am 5 Mär. 2018
@Jan - I didn't realize you could do that!
Lexington Stoyell
Lexington Stoyell am 5 Mär. 2018
How do you then add the zeros on the end?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 5 Mär. 2018
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 Kommentare
Lexington Stoyell
Lexington Stoyell am 6 Mär. 2018
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 am 6 Mär. 2018
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by