how to load values of two matrices into one matrix?

1 vue (au cours des 30 derniers jours)
kavya kolipakula
kavya kolipakula le 25 Fév 2019
Commenté : kavya kolipakula le 25 Fév 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]

Réponse acceptée

Alex Mcaulley
Alex Mcaulley le 25 Fév 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

Plus de réponses (2)

Arthur Nascimento
Arthur Nascimento le 25 Fév 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) le 25 Fév 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]

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by