Clear Filters
Clear Filters

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.

1 view (last 30 days)
% 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
Yatin
Yatin on 18 Oct 2013
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?
sony
sony on 18 Oct 2013
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

sixwwwwww
sixwwwwww on 18 Oct 2013
Edited: sixwwwwww on 18 Oct 2013
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
Image Analyst
Image Analyst on 19 Oct 2013
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).
sony
sony on 20 Oct 2013
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)

Image Analyst
Image Analyst on 18 Oct 2013
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
sony
sony on 18 Oct 2013
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

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!