how to write a function called calcrmse that given 2 input vectors checks to see if the vectors are the same size. in matlab

Here's a MATLAB code that defines the calcrmse function which calculates the Root Mean Squared Error (RMSE) between two input vectors of the same size. If the sizes of the input vectors differ, it returns an error message.

main.m
function rmse = calcrmse(vector1, vector2)

% check if the sizes are equal
if length(vector1) ~= length(vector2)
  error('Input vectors have different sizes!');
end

% calculate the RMSE
squaredError = (vector1 - vector2).^2;
meanSquaredError = mean(squaredError);
rmse = sqrt(meanSquaredError);

end
301 chars
14 lines

You can call this function in MATLAB with two input vectors:

main.m
vector1 = [1 2 3 4 5];
vector2 = [2 4 6 8 10];

rmse = calcrmse(vector1, vector2);
83 chars
5 lines

This will give you the RMSE between vector1 and vector2. Note that the two vectors should be of the same size.

related categories

gistlibby LogSnag