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

1 次查看(过去 30 天)
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.

采纳的回答

rumin diao
rumin diao 2022-9-2
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by