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

2 views (last 30 days)
Saiem Solimullah on 24 Jan 2018
Answered: Jos (10584) on 15 Feb 2018
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 on 24 Jan 2018
Edited: Andrei Bobrov on 24 Jan 2018
bnew = repmat(b,1,numel(a)/numel(b))
Saiem Solimullah on 15 Feb 2018
this code will not work due to its diffrent length of b

Jos (10584) on 15 Feb 2018
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)