how to write a matlab code that sum all nonzero elements that divisible by 5 in a vector
이전 댓글 표시
Write a MATLAB script that inputs a vector V and then displays the sum and product of nonzero elements of V that are divisible by 5.
댓글 수: 1
답변 (1개)
JESUS DAVID ARIZA ROYETH
2018년 5월 6일
편집: JESUS DAVID ARIZA ROYETH
2018년 5월 6일
V=input('input vector: ');
elements=V(and(V~=0,mod(V,5)==0));
fprintf(' sum = %4.2f\n product=%4.2f\n',sum(elements),prod(elements))
execution:
input vector: [5 0 10 2 8] sum = 15.00 product=50.000
댓글 수: 2
sloppydisk
2018년 5월 6일
You forgot the equals zero statement
mod(V,5)==0
Also I don't really see how this will help the asker in any way. You didn't explain anything you just gave the answer.
Rik
2018년 5월 6일
It's generally not the best idea to just give people answers to their homework questions. They won't learn anything, might hand it in as their own work without modification, they will be back for more easy answers, and the benefit to the community as a whole is virtually zero.
카테고리
도움말 센터 및 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!