Combing 2 arrays of different size in matlab

1 view (last 30 days)
Hello,
I have a weird problem and I can not figure out how to go about it.
I have a double array that's 1x8
I need to add another row to this array that's 1x10 and since the old array didn't have values for column 9 and 10, I want that to be NaN
so,
oldarr = [1,2,3,4,5,6,7,8]
newarr = [1,2,3,4,5,6,7,8,9,10]
combinearr =
1 2 3 4 5 6 7 8 NaN NaN
1 2 3 4 5 6 7 8 9 10
How would I do this?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 23 Jun 2015
oldarr = [1,2,3,4,5,6,7,8]
newarr = [1,2,3,4,5,6,7,8,9,10]
n=numel(oldarr)
m=numel(newarr)
nm=max(n,m)
combinearr = [oldarr nan(1,nm-n); newarr nan(1,nm-m)]

More Answers (1)

Steven Lord
Steven Lord on 23 Jun 2015
x = 1:7;
Lx = size(x, 2);
y = 1:5;
Ly = size(y, 2);
M1 = [x, NaN(1, Ly-Lx); ...
y, NaN(1, Lx-Ly)]
M2 = [y, NaN(1, Lx-Ly); ...
x, NaN(1, Ly-Lx)]
The documentation page for NaN includes this note:
"The size inputs sz1,...,szN, as well as the elements of the size vector sz, should be nonnegative integers. Negative integers are treated as 0."
ZEROS, ONES, RAND, etc. behave the same way in case you want different padding.

Categories

Find more on Creating and Concatenating Matrices 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!