フィルターのクリア

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

1 回表示 (過去 30 日間)
Bryce McCord
Bryce McCord 2022 年 9 月 2 日
回答済み: rumin diao 2022 年 9 月 2 日
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 Center および File ExchangeLoops 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