MATLAB: Array operations

arrayshomework

How can I solve this problem using MATLAB? The array n is all integers from 1 to 110. The array y is defined by: y= n^(1/2) if n is divisible by 3, 5, or 7 y= 0 otherwise Find the value of the sum of the elements of y.

Best Answer

num = 1:110;
for i = 1:101 z(i) = num(i)/3; x(i)= num(i)/5; y(i)= num(i)/7;
if rem(z(i),1) == 0; k(i) = num(i)^(1/2);
elseif rem(x(i),1) == 0; k(i) = num(i)^(1/2);
elseif rem(y(i),1) == 0; k(i) = num(i)^(1/2);
else k(i) = 0;
end;
end;
sum = sum(k);