How do I add NaNs to each of a set of vectors of different lengths to make them all the same length please?
12 views (last 30 days)
Show older comments
Michel Nieuwoudt
on 18 Aug 2016
Commented: Bojie Sheng
on 26 Sep 2019
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?
0 Comments
Accepted Answer
Walter Roberson
on 18 Aug 2016
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.
More Answers (2)
Azzi Abdelmalek
on 18 Aug 2016
Edited: Azzi Abdelmalek
on 18 Aug 2016
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 Comment
Bojie Sheng
on 26 Sep 2019
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?
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!