Can anyone help to creat this vector ?

4 views (last 30 days)
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
diadalina on 13 Jan 2018
Edited: James Tursa on 13 Jan 2018
i have tried this but it doen't give me the good result can you help me
V = [5, 17, -3, 8, 0, -1, 12, 15, 20, -6, 6, 4, -7, 16];
for i=1:length(V)
if V(i)> 0 & mod(V(i),3)==0 | mod(V(i),5)==0
if V(i)<0 & V(i)>-5

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 13 Jan 2018
Edited: James Tursa on 13 Jan 2018
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;
W(i) = V(i);
  1 Comment
diadalina on 13 Jan 2018
that's right thank you Mr. James Tursa for helping me.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by