how to load values of two matrices into one matrix?

1 visualizzazione (ultimi 30 giorni)
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]

Risposta accettata

Alex Mcaulley
Alex Mcaulley il 25 Feb 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

Più risposte (2)

Arthur Nascimento
Arthur Nascimento il 25 Feb 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) il 25 Feb 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]

Community Treasure Hunt

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

Start Hunting!

Translated by