function avg = cubes_mean(v) % a counter is needed to have the

advertisement
function avg = cubes_mean(v)
% a counter is needed to have the exact number of positive
% elements of the given array "v".
counter = 0;
% initialize the variable to keep the sum of cubes of positive
elements.
summ = 0;
% move along length of "v".
% in other words, get each element of "v" one by one.
for i = 1:length(v)
% check if the i. element of "v" is positive.
if v(i) > 0
% if the current element is positive,
% then have the cube of it and add to "summ".
summ = summ + v(i)^3;
% also increase the counter by one.
counter = counter + 1;
end
end
end
function result = make_pyramid(a)
% sort the given array using the MatLab function "sort".
a = sort(a);
% initialize 3 empty arrays. one array for the output.
% one array for ascending part.
% other array for descending part.
b = [];
c = [];
d = [];
% get the 1., 3., 5., ... elements of input array.
for i = 1:2:length(a)
% append each element to the array "b".
b = [b a(i)];
end
% divde all the sumation to the number of positive elements of
"v"
% to have the mean value.
avg = summ / counter ;
end
function a = flatten_matrix(b)
% get the 2., 4., 5., ... elements of input array.
for i=2:2:length(a)
% append each element to the array "c".
c =[c a(i)];
end
% sort the elements of "c" in descending order.
% we use the MatLab function "sort".
% for more info, please see "help sort" of MatLab.
c = sort(c,'descend');
% append the array "b" to the array "d".
d = [d b];
% append the array "c" to the array "d".
d = [d c];
% initialize the output array.
a = [];
% output is ready.
result = d;
% get the size of input matrix.
% size (b) returns an array with 2 elements.
% the first element is the row size of the given matrix.
% the second element is the column size of the matrix.
x = size(b);
% plot as histogram.
hist(d,256);
end
function result = pyramidize_rec(nums, level)
% move along all rows.
for i = 1:x(1)
% move along all coulmns.
for j = 1:x(2)
% append each element one by one to the "a" array.
a = [a b(i,j)];
end
end
end
function result = full_squares (n)
% initialize the output as an array.
% the elements of "result" will be changed below.
result = n;
% to check all elements of array "n" we need for loop.
for i=1:length(n)
% get the square root of each element.
a = sqrt(n(i));
% round the square root.
b = round(a);
% if the i. element of "n" is a full-square,
% then a - b shoulde be 0
if a - b == 0
% full square element of "n", so the i. element of "results"
should be 1.
result(i) = 1;
% "0" otherwise.
else
result(i) = 0;
end
end
% if level is equal to 1, then call the previously written
% function "make_pyramid".
if level == 1
d = make_pyramid(nums);
% for level that is bigger than 1, the function uses the below
part.
else
% initialize an empty array.
d = [];
% sort the given array using the MatLab function "sort".
nums = sort(nums);
% if the given level is 2, we need 2 sorted arrays.
% if level is 3, 3 sorted arrays needed.
% etc.
% so we define a for loop.
for j = 1:level
% create an empty array at each time for loop is used.
b = [];
% fill the empty array with the necessary elements of
"nums".
for i = j:level:length(nums)
b = [b nums(i)];
end
% Make a pyramid within each sub-array:
bb = make_pyramid(b);
% Append each pyramidized array to the result aray.
d = [d bb];
end
result = d;
end
end
Download