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

6 次查看(过去 30 天)
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