MATLAB: Hi, I have a problem with dividing in Matlab. when I divide to variables m(1)/m(2) it result in integer, but it’s real value is decimal. how can I get the real value

decimaldividedividingintegerMATLABround

clc
clear all
close all
n=5;
x=[0 .25 .5 .75 1];
for i=1:n
for j=1:n
if i~=j
r(j) = x(i)-x(j);
elseif i==j
r(j) = 1.000000;
end
if j==1
m(i) = r(j);
elseif j>1
m(i) = m(i)*r(j);
end
end
end
m(1)/m(2)

Best Answer

but its real value is decimal
Is it now?
  • m(1) is 1 * -1/4 * -1/2 * -3/4 * -1 = 3/32
  • m(2) is 1/4 * 1 * -1/4 * -1/2 * -3/4 = -3/128
  • m(1) / m(2) = 3/32 * -128/3 = -128/32 = -4
Note that a much faster and simpler way to calculate your m would be:
m = prod(x - x' + eye(numel(x))) %assuming R2016b or later
Related Question