How do I add NaNs to each of a set of vectors of different lengths to make them all the same length please?

조회 수: 14 (최근 30일)
I have 13 columns that I'd like to put into a matrix, but they are different lengths. I can add NaN to them to make them the same length; if the max length is say 5000, how do I add NaNs to the others to make them all 5000 please?

채택된 답변

Walter Roberson
Walter Roberson 2016년 8월 18일
Matrix = nan(5000,13);
Matrix(1:length(FirstVariable), 1) = FirstVariable;
Matrix(1:length(SecondVariable), 2) = SecondVariable;
and so on.
If the values were in a cell array then there would be other methods available as well.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 18일
편집: Azzi Abdelmalek 2016년 8월 18일
v1=(1:5)';
v2=(1:8)';
v3=(1:7)'
n=10
A={v1 v2 v3 }
out=cell2mat(cellfun(@(x) [x;nan(n-numel(x),1)],A,'un',0) )
  댓글 수: 1
Bojie Sheng
Bojie Sheng 2019년 9월 26일
Hi how can I add nan at the end of each row, not each coloum. For example:
v1=(1:5);
v2=(1:8);
v3=(1:7);
n=10;
A={v1; v2; v3; };
How to use cell2mat?

댓글을 달려면 로그인하십시오.


Michel Nieuwoudt
Michel Nieuwoudt 2016년 8월 18일
Thank you for your answer, however, I'm not sure I understand the last line. (I am still learning!)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by