I have a matrix 'a' and i want to calculate the distance from one point to all other points. So really the outcome matrix should have a zero (at the point I have chosen) and should appear as some sort of circle of numbers around that specific point.

% this is what i have so far but its not showing me what i want. I cant figure out what im doing wrong.
%any suggestions would be extremely helpful. Thank you in advance :)
a = [1 2 3 4 5 6 7 8 9 10]
for i = 2:20
a(i,:) = a(i-1,:) + 1;
end
N = 10
for I = 1:N
for J = 1:N
dx = a(I,1)-a(J,1);
dy = a(I,2)-a(J,2);
distance(I,J) = sqrt(dx^2 + dy^2)
end
end

2 Comments

Is the vector a, the set of all points that you have? I am not able to understand what are you trying to do in the first for loop by creating a matrix of 20 rows. Can you explain in more details as to what are you trying to achieve here?
Please accept my apology for the lack of explanation but im struggling to explain what I want. The first loop is just to create a matrix (its not relevant). I have attached a pdf of the explanation. I hope this is more helpful. Thank you

Sign in to comment.

 Accepted Answer

Dear Sarwar, following is an example code as you desired:
A = rand(5, 5);
select_cell = [3 3];
distance = zeros(size(A, 1), size(A, 2));
for i = 1:size(A, 1)
for j = 1:size(A, 2)
distance(i, j) = sqrt((i - select_cell(1))^2 + (j - select_cell(2))^2);
end
end
disp(distance)
I hope it helps. Good luck!

5 Comments

You could improve your code if you learn to vectorize. I did your slow 7 line double for loop in one line (no loops):
distances = sqrt((x-xCenter).^2+(y-yCenter).^2)
Thank you very much guys! You both have been amazingly helpful.
I know you accepted this Answer as the as the preferred answer, perhaps because it's more straightforward and intuitive for beginners, however I'd really like you to consider using the vectorized approach I gave. It's the more efficient, faster, MATLAB-ish approach and is the one you'll find all experienced MATLAB programmers using . Vectorization is really what you need to be doing as you create programs in MATLAB, and if you don't do that you're ignoring one of the benefits of MATLAB and using it as just another dumb, lower level language. If you don't understand the dot-multiply concept then you can read the Getting Started section of the help (I'm sure it must be in there).
Thank you for the advice. You are correct, i chose that answer because it gave me what i wanted to know. However i will look into vectorisation as i am keen to learn this software. Thanks

Sign in to comment.

More Answers (1)

Do you mean like this:
% Make a set of 50 points
x = rand(1, 50);
y = rand(1, 50);
% Now make a point that we want to use as the reference
% from which we will calculate distances and plot them.
xCenter = mean(x);
yCenter = mean(y);
% Now find the distances
distances = sqrt((x-xCenter).^2+(y-yCenter).^2)
% Now plot the lines
for k = 1 : length(x)
% Plot the line
line([x(k), xCenter], [y(k), yCenter], 'LineWidth', 3);
if k == 1
hold on;
end
% Plot the endpoints as markers.
plot(x(k), y(k), 'or', 'LineWidth', 3);
end
% Plot the reference point as a marker.
plot(xCenter, yCenter, 'or', 'LineWidth', 3);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

2 Comments

Or maybe you mean this: http://matlab.wikia.com/wiki/FAQ?&cb=1314#How_do_I_create_a_circle.3F Why don't you explain your question better and include a screenshot of what you'd like to obtain?
Please accept my apology for the lack of explanation but im struggling to explain what i want. I have attached a pdf of the explanation. I hope this is more helpful. Thank you

Sign in to comment.

Categories

Asked:

on 18 Oct 2013

Commented:

on 20 Oct 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!