# Double loop sort and group results in one matrix

2 ビュー (過去 30 日間)
Avik Mahata 2021 年 11 月 18 日

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

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

### 回答 (1 件)

Prateek Rai 2021 年 11 月 21 日
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.

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

### カテゴリ

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!