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

조회 수: 6 (최근 30일)
Mia Dier
Mia Dier 2021년 1월 15일
댓글: Mia Dier 2021년 1월 15일
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

채택된 답변

Daniel Pollard
Daniel Pollard 2021년 1월 15일
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?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by