how to load values of two matrices into one matrix?

1 visualización (últimos 30 días)
kavya kolipakula
kavya kolipakula el 25 de Feb. de 2019
Comentada: kavya kolipakula el 25 de Feb. de 2019
let A=[1;2;3;4;5]
B=[6;7;8;9;10]
and I want h=[1;6;2;7;3;8;4;9;5;10]

Respuesta aceptada

Alex Mcaulley
Alex Mcaulley el 25 de Feb. de 2019
tmp = 1:length(A)+length(B);
h = zeros(length(A)+length(B),1)
h(mod(tmp,2)==1) = A
h(mod(tmp,2)==0) = B

Más respuestas (2)

Arthur Nascimento
Arthur Nascimento el 25 de Feb. de 2019
A=[1;2;3;4;5]
B=[6;7;8;9;10]
for i=1:length(A)*2
if mod(i,2)==0
h(i)=B(ceil(i/2))
else
h(i)=A(ceil(i/2))
end
end

Jos (10584)
Jos (10584) el 25 de Feb. de 2019
Many roads to Rome, which all learn you about using transpose, reshape, concatention, and/or clever indexing
H1 = reshape([A B].', [], 1) % when A and B have the same lengths
H2([1:2:2*numel(A) 2:2:2*numel(B)+1], 1) = [A ; B]

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by