Can anyone help to creat this vector ?
조회 수: 4 (최근 30일)
이전 댓글 표시
A vector is given by: V = [5; 17; 3; 8; 0; 1; 12; 15; 20; 6; 6; 4; 7; 16]. Write a program in the form of a script file, which doubles the positive elements that are divisible by 3 and / or by 5, and raises to the cube the negative elements greater than -5 ie: WI= 1/ 2*Vi if Vi>0 multiple of 3 and / or 5 2/ (Vi)^3 if Vi<0 greater than -5 3/ Vi otherwise
댓글 수: 3
채택된 답변
James Tursa
2018년 1월 13일
편집: James Tursa
2018년 1월 13일
You're close, but you need to handle the W(i)=V(i) differently. The way you have it currently coded, this line is overwriting all of your other code. Maybe use some "elseif" clauses. Also you should probably use the scalar logical operators "&&" and "||" for this and parentheses to make the logical test exactly what you want. The parentheses are particularly important to use when you have multiple comparisons going on within one test to make sure the order they are evaluated in is exactly what you want. Getting in the habit of using the scalar logical operators "&&" and "||" instead of the element-wise operators "&" and "|" will become important when you forget at some point in the future to use subscripts. The "&&" and "||" will generate an error which will give you an immediate feedback that something is wrong with your code and exactly where it is wrong in your code. The "&" and "|" will often simply result in a wrong answer and then you have to track down where your code went wrong. E.g.,
if V(i)> 0 && (mod(V(i),3)==0 || mod(V(i),5)==0)
W(i) = V(i)*2;
elseif V(i)<0 && V(i)>-5
W(i) = V(i)^3;
else
W(i) = V(i);
end
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!