Create a loop that performs calculation on 100 rows and moves to the next 100 rows in a matrix.

3 views (last 30 days)
I have a matrix q= 11700x3 and want to perform the following calculation for every 100 rows and than it moves to next 100 rows
(e.g. From row 1 to 101, 102 to 202 and so on), so that will give a 117x1 matrix (vector).
The following code gives a value for the first 100 rows only.
% q=matrix of size 11700x3
n=100;
ANS=0;
for i=1:(n-1)
for j=2:n
v=[q(i,:)-q(j,:)].^2;
ANS=ANS+v;
rsum=ANS(:,1)+ANS(:,2)+ANS(:,3);
rsqt=sqrt(rsum);
end
end
dc=(2/((n-1)*(n)))*rsqt
Thanks in advance.

Accepted Answer

Torsten
Torsten on 29 Sep 2022
n = 100;
m = 11700/n;
dc = zeros(m,1);
for count = 1:m
M = q((count-1)*n+1:count*n,:);
summe = 0.0;
for i = 1:n-1
for j = i+1:n
summe = summe + sqrt( (M(i,1)-M(j,1))^2 + (M(i,2)-M(j,2))^2 + (M(i,3)-M(j,3))^2 );
end
end
dc(count) = summe * 2/(n*(n-1));
end

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!