Also Read
Set even values zero and other multiplied by 3
clear allclcA= [3 15 27; 6 18 30; 9 21 33; 12 24 36]; % Vector declarationdisp(A); %Display Vector before performing operationfor i = 1 : size(A) for k = 1: 3if rem(A(i,k), 2) == 0 %If number is even, then set it to 1A(i, k) = 1;else A(i,k) = A(i,k)*3; %else multiple by 3end endend disp(A) %Display vector after operation
Set the positive values of x to zero
disp(x); %Display Vector before performing operation
for i = 1 : size(x, 2)
if x(1, i) > 0 %if number is greater than 0, then set it to 0
x(1, i) = 0;
end
end
disp(x); %Display vector after operation
Set values that are multiples of 3 to 3
x= [6 20 8 13 -5 -4 0 12 7 9 3 2]; % Vector declaration
disp(x); %Display Vector before performing operation
for i = 1 : size(x, 2)
if rem(x(1, i), 3) == 0 %If number is multiple of 3, then set it to 3
x(1, i) = 3;
end
end
disp(x); %Display vector after operation
Multiply the even values of x by 7
disp(x); %Display Vector before performing operation
for i = 1 : size(x, 2)
if rem(x(1, i), 2) == 0 %if number is even, then multiply it by 7
x(1, i) = x(1, i)*7;
end
end
disp(x); %Display vector after operation
Extract the values of x that are greater than 5 into a vector called y
x= [6 20 8 13 -5 -4 0 12 7 9 3 2]; % Vector declaration
y = []; %Create empty vector to store result
j = 1;
for i = 1 : size(x, 2)
if x(1, i) > 5 %if number is greater than 5, then store it in vector y
y(1, j) = x(1, i);
j = j + 1;
end
end
disp('the new y matrix is:')
disp(y);
Select that larger value of x and print it into vector called z
x= [6 20 8 13 -5 -4 0 12 7 9 3 2]; % Vector declaration
y = []; %Create empty vector to store result
y = max(x)
disp('the new y matrix is:')
disp(y)
Set the values in x that are less than the mean to zero
x= [6 20 8 13 -5 -4 0 12 7 9 3 2]; % Vector declaration
disp(x); %Display Vector before performing operation
sum = 0;
for i = 1 : size(x, 2) %Get the sum of all numbers
sum = sum + size(1, i);
end
mean = sum / size(x, 2); %Calculate Mean
for i = 1 : size(x, 2)
if x(1, i) < mean %Set all the numbers that are less than mean to 0
x(1, i) = 0;
end
end
disp(x) %Display vector after operation
Set the values in x that are above the mean to their difference from the mean
x= [6 20 8 13 -5 -4 0 12 7 9 3 2]; % Vector declaration
disp(x); %Display Vector before performing operation
sum = 0;
for i = 1 : size(x, 2) %Sum of all the numbers
sum = sum + size(1, i);
end
mean = sum / size(x, 2); %Calculate Mean
for i = 1 : size(x, 2)
if x(1, i) > mean %Set all the numbers that are greater than mean to 0
x(1, i) = 0;
end
end
disp(x); %Display vector after operation
Comments