What does the code below do? clear vec = randi(20,1,100)-10; iter=0; k=1; while k<= length(vec) if vec(k)<0 vec(k)= vec(k) * -1; end k = k+1; end
What does the code below do?
clear
vec = randi(20,1,100)-10;
iter=0;
k=1;
while k<= length(vec)
if vec(k)<0
vec(k)= vec(k) * -1;
end
k = k+1;
end
This code generates a
This code generates a vector with 20 random integer elements between -100 and 100, and replaces all the negative integers with a positive integer of the same absolute value.
This code generates a vector with 100 random integer elements between -10 and 10, and changes the sign of all negative elements.
This code generates a vector with 100 random integer elements between 20 and 100, and changes the sign of the elements coverting all poivitive ones to negative and all negative ones to positive.
This code generates a vector with 20 random integer elements between 1 and 100, and changes the sign of all the elements.
The code
clear
vec = randi(20,1,100)-10;
iter=0;
k=1;
while k<= length(vec)
if vec(k)<0
vec(k)= vec(k) * -1;
end
k = k+1;
end
This code do :
This code generates a vector with 20 random integer elements between 1 and 100, and changes the sign of all negative elements.
Step by step
Solved in 2 steps