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

5 Ansichten (letzte 30 Tage)
Mia Dier
Mia Dier am 15 Jan. 2021
Kommentiert: Mia Dier am 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

Akzeptierte Antwort

Daniel Pollard
Daniel Pollard am 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?

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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