% default stuff clear; clc; clf; % this is the original height of 3*5 land land = [1 2 3 4 5; 2 3 4 5 6; 7 7 7 7 7]; % visualize the original landscape first =) surf(land) % or you can use 'mesh' % this is the desired height of that 3*5 land desired_height = input('What is your desired flat land height (in m): '); desired = repmat(desired_height,3,5); % show the desired flat landscape! hold on % do google search/browse Matlab help to understand what is this surf(desired) % aha, you see overlapping figures now hold off % now the computation part, in step by step fashion % we want to know the height difference in each square meter! % % since the required solution is the 'minimum' amount of soil to be % added or removed, then we must use soil from a square that is % 'higher' from the desired height and dump it into square that is % 'lower' than the desired height. difference = desired - land; columnSum = sum(difference); % we sum column by column % this is the amount of soil to be added (if positive) % or removed (if it is negative) finalAnswer = sum(columnSum); % you can also write all in one line, like this: % finalAnswer = sum(sum(desired-land)); % to make the output a little bit nicer, use 'if', 'disp', and 'sprintf' if finalAnswer > 0 % positive, means add disp(sprintf('We need to add %d m^3 soil', finalAnswer)); else % negative, means remove, make the answer positive! disp(sprintf('We need to remove %d m^3 soil', -finalAnswer)); end % challenge: % play around with mesh/surf function, it is a powerful % visualization tool!