MATLAB: How to write a for loop to calculate the difference between two points

difference arrays

Here's what I have so far:
% Write a program using a for loop to get user input for a nX2 array, then find the difference between point 1 and the rest.
n=input('how many points do you have? ');
A=zeros (n,2);
for row=1:n
for column=1:2
usernumber=input('enter a number: ');
A(row, column)=usernumber;
end
end
A
for row=1:length(A(n,2))
Dist=sqrt((A(n,1)-A(n+1,1))^2)+((A(n+1,2)-A(n,2))^2)
end
Dist
I'm not sure how to find the differences through the loop and then store them

Best Answer

Dist = zeros(size(A,1),1);
for jj = 2:numel(Dist)
Dist(jj) = sqrt(sum((A(jj,:) - A(1,:)).^2,2));
end
or
Dist = sqrt(bsxfun(@minus,A,A(1,:)).^2*[1;1]);
or
A1 = num2cell(bsxfun(@minus,A,A(1,:)),1);
Dist = hypot(A1{:});
Related Question