Can anyone help to creat this vector ?

3 次查看(过去 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 个评论
diadalina
diadalina 2018-1-13
I want with a given vector V =[5, 17, -3, 8, 0, -1, 12, 15, 20, -6, 6, 4, -7, 16], i want to creat W ie: Wi= 2*Vi if Vi>0 multiple of 3 and / or 5 the second case Wi= (Vi)^3 if Vi<0 greater than -5 the third case Wi=Vi otherwise , can you help me please ?
diadalina
diadalina 2018-1-13
编辑:James Tursa 2018-1-13
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
W(i)=V(i)*2;
end
if V(i)<0 & V(i)>-5
W(i)=V(i)^3;
end
W(i)=V(i);
end

请先登录,再进行评论。

采纳的回答

James Tursa
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 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by