필터 지우기
필터 지우기

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