Info

This question is closed. Reopen it to edit or answer.

calculate distances between the rows of a matrix through an iterative process

1 view (last 30 days)
Hello,
Consider a matrix 100x10, denoted as A, and the first row of A
row1=A(1,:);
I calculate the Euclidean distances between row1 and A and I found the closest row to row1, denoted as row_a.
Then I exclude the row1 from A (which becomes 99x10) and calculate the distances between row_a and the new A.
I find again the closest row to row_a, denoted as row_b, and exclude the row_a from the new A (which becomes 98x10). Then I calculate the distances between row_b and the new A and so on.
How can I do this iterative process with a loop?
Thank you very much.
Best,
Natasha

Answers (1)

Sean de Wolski
Sean de Wolski on 17 Sep 2012
Build an index vector of rows to keep when doing the comparison: pseudocode
row_of_interest = 10; %initial row of interest
rows_to_keep = 1:100; %rows that have not yet been the closest
rows_closest_storage = zeros(1,100); %storage vector for closest rows
for ii = 1:99
row_closest = compare_rows_etc. %get the closest row
rows_to_keep(rows_to_keep==row_of_interest) = []; %remove the original row_of interest
rows_closest_storage(ii) = row_of_interest; %store it
row_of_interest = row_closest; %continue
end
  2 Comments
Natasha Kratowska
Natasha Kratowska on 17 Sep 2012
Thank you for your response.
I use the pseudo-code but i get the message:
Error using == Matrix dimensions must agree.
The message appears after the 3rd iteration (ii=3) from the ii=1:99.
Natasha Kratowska
Natasha Kratowska on 17 Sep 2012
This is my code:
row_of_interest = 10;
rows_to_keep = 1:100;
rows_closest_storage = zeros(1,100);
for ii = 1:99
row_closest = pdist2(A(ii,:),A(row_of_interest,:));
rows_to_keep(rows_to_keep==row_of_interest) = [];
rows_closest_storage(ii) = row_of_interest;
row_of_interest = row_closest;
end

Community Treasure Hunt

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

Start Hunting!