How do i combine two different sized vectors?
Show older comments
A = [1;3;5;7;9;11] and B = [2;4;6;8;10;12;14;16]
to make C = [1;2;3;4;5;6;7;8;9;10;11;12;14;16]
Accepted Answer
More Answers (1)
It is possible. Assuming, that you data do not contain NaNs:
X1 = [3;2;1;-1;2;3];
Y1 = [3.5;2.5;1.5;-1.5;2.5;3.5];
function C = JoinWithOrder(X, Y)
nX = numel(X);
nY = numel(Y);
C = NaN(2, max(nX, nY));
C(1, 1:nY) = Y(:).';
C(2, 1:nX) = X(:).'; % [EDITED, "C(1" -> "C(2", thanks Andrei!
C = C(~isnan(C));
end
2 Comments
Andrei Bobrov
on 15 Nov 2017
Hi Jan! Maybe so?
C(1, 1:nY) = Y(:).';
C(2, 1:nX) = X(:).';
or
C(2, 1:nY) = Y(:).';
C(1, 1:nX) = X(:).';
Jan
on 15 Nov 2017
@Andrei: Yes. I assume the shown data match to setting the Y values in the first line - perhaps.
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!