How to make each matrix in a cell of equal size by adding NaN?

How may I make each matrix in a cell of equal size by adding NaN? My data is a 1000x1 cell containing 'nx2' sized matrices and looks like this:
A =
[8x2 double]
[6x2 double]
[9x2 double]
[6x2 double]
[7x2 double]
...

 Respuesta aceptada

An approach:
A={rand(8,2);rand(6,2);rand(9,2);rand(6,2);rand(7,2)};
sizesRow=cellfun(@(x) size(x,1),A);
addition=max(sizesRow)-sizesRow;
for i=1:size(A,1)
A{i}(sizesRow(i)+1:sizesRow(i)+addition(i),:)=NaN;
end

3 comentarios

Jan
Jan el 19 de En. de 2018
Editada: Jan el 19 de En. de 2018
More efficient:
sizesRow = cellfun('size', A, 1);
cellfun is slow with anonymous functions.
You calculate addition=max(sizesRow)-sizesRow at first and sizesRow(i)+addition(i) inside the loop. Easier:
width = max(sizesRow);
for k = 1:size(A,1)
A{k}(sizesRow(k)+1:width, :) = NaN;
end
Thanks Jan.
Thank you guys. @Birdman @Jan Simon

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 19 de En. de 2018

Comentada:

el 19 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by