MATLAB: Im trying to find the absolute value of the difference between current value ‘c’ and the values ‘n’.

absoluteerrorfor loopfunctions

Im trying to find the absolute value of the difference between current value 'c' and the values 'n'. Matlab keeps coming back saying 'Subscripted assignment dimension mismatch.'
E=[363725;142414;795692;184315];
c=E(3,1);
n = [4;9;8]
for i=1:length(n)
N(1,i) = abs(c-n(1,i));
end
What is wrong with the second line in my for loop?

Best Answer

You say n(1,i), meaning the 3rd column of the first row. However, n is a column vector with only one column, not 3. Don't use 1, or ,1 when dealing with row or column vectors - it's not needed and just complicates things.
The fix:
E=[363725;142414;795692;184315]
c=E(3)
n = [4;9;8]
for i=1:length(n)
N(i) = abs(c-n(i))
end