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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
% 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 Kommentare
Yatin
am 18 Okt. 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?
Akzeptierte Antwort
sixwwwwww
am 18 Okt. 2013
Bearbeitet: sixwwwwww
am 18 Okt. 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 Kommentare
Image Analyst
am 19 Okt. 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).
Weitere Antworten (1)
Image Analyst
am 18 Okt. 2013
Do you mean like this:
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
% 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 Kommentare
Image Analyst
am 18 Okt. 2013
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?
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!