Array operations

조회 수: 5 (최근 30일)
Yassine
Yassine 2012년 3월 22일
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
James Tursa
James Tursa 2012년 3월 22일
Yes, n is those numbers.
Andrei Bobrov
Andrei Bobrov 2012년 3월 23일
out = sum(sqrt(num(all(bsxfun(@rem,num.',[3 5 7]),2))))

댓글을 달려면 로그인하십시오.

채택된 답변

yasmine
yasmine 2012년 3월 22일
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
Yassine
Yassine 2012년 3월 23일
thanks for the answer, but what does '1' mean in the expression rem(Z(i),1)?
Jan
Jan 2012년 3월 23일
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
...

카테고리

Help CenterFile 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