Double loop sort and group results in one matrix

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
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.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 11월 18일

답변:

2021년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by