How do I rewrite this code to output the same answer without the for loops

1 Ansicht (letzte 30 Tage)
A = [1 3 5 7 9];
B = [2 4 6 8 10];
L = min([length(A) length(B)]);
C = zeros(1, 2*L);
for i=1:L
C(2*(i-1)+1) = A(i);
C(2*i) = B(i);
end
disp('C = ');
disp(C);
I need the same output accomplished without the presence of a for loop i.e. a vector in the form of [a1 b1 a2 b2 a3 b3]. Thanks for the help.

Akzeptierte Antwort

rumin diao
rumin diao am 2 Sep. 2022
it seems you can delete the for loop and make i a vector:
A = [1 3 5 7 9];
B = [2 4 6 8 10];
L = min([length(A) length(B)]);
C = zeros(1, 2*L);
% for i=1:L
% C(2*(i-1)+1) = A(i);
% C(2*i) = B(i);
% end
i=1:L;
C(2*(i-1)+1) = A(i);
C(2*i) = B(i);
disp('C = ');
disp(C);

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by