How to add or multiply two different size of array using the loop iteration process?

How to add or multiply two different size of array using the loop iteration process? suppose ,an array
a=[1 2 3 4 5 6 7 8 9 ]
b=[1 2 3 ]
and i want increase the size of the 'b' array using. for loop. as like the value of 'b' after using for loop is
= [1 2 3 1 2 3 1 2 3]
Then, we will add or multiply this array.That will very much appreciating if anyone can give a matlab code for this problem....

 採用された回答

Andrei Bobrov
Andrei Bobrov 2018 年 1 月 24 日
編集済み: Andrei Bobrov 2018 年 1 月 24 日
bnew = repmat(b,1,numel(a)/numel(b))

3 件のコメント

Saiem Solimullah
Saiem Solimullah 2018 年 1 月 30 日
this is working but how can i do this process by using for loop??
na = numel(a);
nb = numel(b);
bnew = a;
for ii = 1:na/nb
bnew(nb*(ii-1)+1 : nb*ii) = b;
end
Saiem Solimullah
Saiem Solimullah 2018 年 2 月 15 日
this code will not work due to its diffrent length of b

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2018 年 2 月 15 日
a = [1 2 3 4 5 6 7 8 9 ]
b = [1 2 3 4] % Generic case: numel(a) is not a multiple of numel(b)
nb = numel(b) ;
for ka=1:numel(a)
kb = rem(ka-1,nb)+1 ;
c(ka) = a(ka) * b(kb) ;
end
But this can be vectorised, to make it more efficient:
kb = rem(0:numel(a)-1,nb)+1
c = a .* b(kb)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by