# Double loop sort and group results in one matrix

2 views (last 30 days)
Avik Mahata on 18 Nov 2021
Answered: Prateek Rai on 21 Nov 2021
I have two matrices X and Y, both 3x100 columns. I am performing some operation between the elements (cartesian distance, not important for this problem), and each step gives 1x100 vector/matrix. Each of the element in X performing an operation on each of the elemt on Y. So X(1) gives a 1x100 vector/matrix. I have to repeat it for all 100 element of X, so I should get a 100x100 matrix. I have the below code, what can I do to get that 100x100 matrix.
L = length(X);
for i=1:L
for j=1:L
X(i)= sqrt((X(j,1)-Y(i,1))^2 + (X(j,2)-Y(i,2))^2 + (X(j,3)-Y(i,3))^2);
X(i) = X(i)/10;
end
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Prateek Rai on 21 Nov 2021
Hi,
A possible workaround to solve the problem could be:
Z = zeros(100,100);
L = length(X);
for i=1:L
for j=1:L
Z(i,j) = sqrt((X(1,j)-Y(1,i))^2 + (X(2,j)-Y(2,i))^2 + (X(3,j)-Y(3,i))^2);
Z(i,j) = Z(i,j)/10;
end
end
Here, Z the 100x100 matrix.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!