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

7 views (last 30 days)
Yaseen on 29 Sep 2022
Commented: Yaseen on 29 Sep 2022
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

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
Yaseen on 29 Sep 2022
Thanks a millon!

### Categories

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!

Translated by