How to add zeros to the end of cells
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Walter Roberson
el 5 de Mzo. de 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 comentarios
Walter Roberson
el 6 de Mzo. de 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);
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!