Array operations

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.

4 个评论

What code have you written so far? Which parts are you stuck on?
I can't understand is n these numbers:
ans =
Columns 1 through 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Columns 19 through 36
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Columns 37 through 54
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
Columns 55 through 72
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
Columns 73 through 90
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
Columns 91 through 108
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
Columns 109 through 110
109 110
or random generated
Yes, n is those numbers.
out = sum(sqrt(num(all(bsxfun(@rem,num.',[3 5 7]),2))))

请先登录,再进行评论。

 采纳的回答

yasmine
yasmine 2012-3-22

0 个投票

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);

2 个评论

thanks for the answer, but what does '1' mean in the expression rem(Z(i),1)?
Please read the documentation: type "help rem" in the command window.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2012-3-22
Start with:
n = 1:110;
Now remember that ^(1/2) is the squareroot.
How can you check, if the numbers in a vector can be divided by 3, 5, or 7? Look e.g. in the help for rem:
doc rem
Another idea:
x = 1:3:20
...

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by