Respuesta aceptada

Birdman
Birdman el 15 de Nov. de 2017
Editada: Birdman el 15 de Nov. de 2017
C=sort([A;B])

7 comentarios

Thankyou, what if I had want to combine two positive/negative/positive vectors:
X1 = [3;2;1;-1;2;3]
Y1 = [3.5;2.5;1.5;-1.5;2.5;3.5]
Same thing:
C=sort([X1;Y1])
George Tod
George Tod el 15 de Nov. de 2017
Editada: Stephen23 el 15 de Nov. de 2017
So that they retain their initial order?
C = [3.5;3;2.5;2;1.5;1;-1;-1.5;-2;-2.5;-3;-3.5]
Do you want to sort them or not? If not, type
C=[X1;Y1]
sorry meant:
C = [3.5;3;2.5;2;1.5;1;-1;-1.5;2;2.5;3;3.5]
Andrei Bobrov
Andrei Bobrov el 15 de Nov. de 2017
It's new question!
@George Tod: can you please explain the logic of the order of C, which swaps order half-way through:
>> Z = [Y(1);X(1);Y(2);X(2);Y(3);X(3);X(4);Y(4);X(5);Y(5);X(6);Y(6)]
Z =
3.5000
3.0000
2.5000
2.0000
1.5000
1.0000
-1.0000
-1.5000
2.0000
2.5000
3.0000
3.5000
>> isequal(C,Z)
ans = 1
Why does Y(4) not follow X(3) ?

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 15 de Nov. de 2017
Editada: Jan el 15 de Nov. de 2017
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 comentarios

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
Jan el 15 de Nov. de 2017
@Andrei: Yes. I assume the shown data match to setting the Y values in the first line - perhaps.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 15 de Nov. de 2017

Comentada:

Jan
el 15 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by