how to load values of two matrices into one matrix?

1 Ansicht (letzte 30 Tage)
kavya kolipakula
kavya kolipakula am 25 Feb. 2019
Kommentiert: kavya kolipakula am 25 Feb. 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]

Akzeptierte Antwort

Alex Mcaulley
Alex Mcaulley am 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

Weitere Antworten (2)

Arthur Nascimento
Arthur Nascimento am 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) am 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]

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by