Double loop sort and group results in one matrix

1 visualizzazione (ultimi 30 giorni)
Avik Mahata
Avik Mahata il 18 Nov 2021
Risposto: Prateek Rai il 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

Risposte (1)

Prateek Rai
Prateek Rai il 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.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by