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 Commenti

James Tursa
James Tursa il 22 Mar 2012
What code have you written so far? Which parts are you stuck on?
Aldin
Aldin il 22 Mar 2012
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
James Tursa
James Tursa il 22 Mar 2012
Yes, n is those numbers.
Andrei Bobrov
Andrei Bobrov il 23 Mar 2012
out = sum(sqrt(num(all(bsxfun(@rem,num.',[3 5 7]),2))))

Accedi per commentare.

 Risposta accettata

yasmine
yasmine il 22 Mar 2012

0 voti

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 Commenti

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

Accedi per commentare.

Più risposte (1)

Jan
Jan il 22 Mar 2012

0 voti

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
...

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by