3. Create vector V of integers randomly distributed on [-10 20] .display the vector. a) Use for-end loop to examine each element of the vector. If element is positive double its value ; if element is negative triple its value. Display the result. b) without using for loop find number of elements in V that are greater than 1 c) without using for loop find all of elements in V that are greater or equal to -7 and are less or equal to 15
MATLAB Code:
close all
clear
clc
V = -10 + (20 - (-10))*rand(1, 10)
W = V;
for i = 1:length(W)
if W(i) > 0
W(i) = 2*W(i);
else
W(i) = 3*W(i);
end
end
disp('Result of Part (a):'), disp(W)
l = length(find(V > 1));
disp('Result of Part (b):'), disp(l)
W = V((V >= -7) & (V <= 15));
disp('Result of Part (c):'), disp(W)
Output:
V =
-7.7180 -7.3168 7.5850 -5.2357 6.7987 3.0948 3.0226 -2.0542 13.7231
5.4227
Result of Part (a):
-23.1541 -21.9505 15.1700 -15.7070 13.5974 6.1896 6.0453 -6.1625
27.4462 10.8454
Result of Part (b):
6
Result of Part (c):
7.5850 -5.2357 6.7987 3.0948 3.0226 -2.0542 13.7231
5.4227
Get Answers For Free
Most questions answered within 1 hours.