フィルターのクリア

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 回表示 (過去 30 日間)
% 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 件のコメント
Yatin
Yatin 2013 年 10 月 18 日
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 2013 年 10 月 18 日
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

サインインしてコメントする。

採用された回答

sixwwwwww
sixwwwwww 2013 年 10 月 18 日
編集済み: sixwwwwww 2013 年 10 月 18 日
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 件のコメント
Image Analyst
Image Analyst 2013 年 10 月 19 日
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 2013 年 10 月 20 日
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

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 10 月 18 日
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 件のコメント
Image Analyst
Image Analyst 2013 年 10 月 18 日
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?
sony
sony 2013 年 10 月 18 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by