Write a program that generates a vector v with 30 random int

Write a program that generates a vector v with 30 random integers between -20 and 20 using the randi () function and then finds the sum s of the all the elements that are divisible by 3. You can use the rem O0 function in MATLAB but Do Not Use the sum) function in MATLAB. Only, the vector V and the sum S must be displayed in the Command Window. Do Not Use di sp or fprintf in your program.

Solution

V = randi([-20,20],1,30); % Generate 30 random numbeers in range -20 and 20
R= rem(V,3); % Find remainder of each element in V and 3
X= V(find(R==0)); % Get values from V which are divisible by 2
S=0;
for i=1:size(X,2)
S=S+X(i);
end

V
S


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site