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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by