How to add a NaN row at the beginning columns of a table?

6 visualizaciones (últimos 30 días)
Mia Dier
Mia Dier el 15 de En. de 2021
Comentada: Mia Dier el 15 de En. de 2021
I have a code that looks like this:
A(:,1)=[NaN;B(1:end-1)];
A(T.id(1:end)~=[NaN;T.id(1:end-1)],1)=NaN;
A(:,2)=[NaN;NaN;B(1:end-2)];
A(T.id(1:end)~=[NaN;NaN;T.id(1:end-2)],2)=NaN;
A(:,3)=[NaN;NaN;NaN;B(1:end-3)];
A(T.id(1:end)~=[NaN;NaN;NaN;T.id(1:end-3)],3)=NaN;
A(:,4)=[NaN;NaN;NaN;NaN;B(1:end-4)];
A(T.id(1:end)~=[NaN;NaN;NaN;NaN;T.id(1:end-4)],4)=NaN;
A(:,5)=[NaN;NaN;NaN;NaN;NaN;B(1:end-5)];
A(T.id(1:end)~=[NaN;NaN;NaN;NaN;NaN;T.id(1:end-5)],5)=NaN;
I want to turn this into a more compact code by adding NaNs to each row sequentially in a loop at each step but I couldn't come up with a code that can do that

Respuesta aceptada

Daniel Pollard
Daniel Pollard el 15 de En. de 2021
for jj = 1:5
A(:,jj) = [NaN([jj 1]); B(1:end-jj)]
A(T.id(1:end) ~= [NaN([jj 1]); T.id(1:end-jj)], jj) = NaN;
end
I haven't tested it but something like this is what you're after?

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by