Can anyone help to creat this vector ?

3 ビュー (過去 30 日間)
diadalina
diadalina 2018 年 1 月 13 日
編集済み: James Tursa 2018 年 1 月 13 日
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
  1 件のコメント
diadalina
diadalina 2018 年 1 月 13 日
that's right thank you Mr. James Tursa for helping me.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by