Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Mia Dier
Mia Dier le 15 Jan 2021
Commenté : Mia Dier le 15 Jan 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

Réponse acceptée

Daniel Pollard
Daniel Pollard le 15 Jan 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?

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by